python - Pandas:在 CSV 文件中使用整个字符串作为分隔符

标签 python regex pandas

在我的 CSV 文件中,每一行都用分隔符 "$$$Field$$$" 分隔(简单字符串,不是正则表达式)。我正在尝试执行以下操作,但它忽略了分隔符。

df = pd.read_csv('filename.csv', sep='\b$$$Field$$$\b')

有什么想法吗?

最佳答案

看来你需要通过\转义$:

import pandas as pd
from pandas.compat import StringIO

temp=u"""Food$$$Field$$$Taste
Apple$$$Field$$$a
Banana$$$Field$$$b"""
#after testing replace 'StringIO(temp)' to 'filename.csv'
df = pd.read_csv(StringIO(temp), sep='\$\$\$Field\$\$\$',engine='python')
print (df)
     Food Taste
0   Apple     a
1  Banana     b

read_csv文档:

sep

: str, defaults to ',' for read_csv(), \t for read_table()

Delimiter to use. If sep is None, will try to automatically determine this. Separators longer than 1 character and different from '\s+' will be interpreted as regular expressions, will force use of the python parsing engine and will ignore quotes in the data. Regex example: '\r\t'.

关于python - Pandas:在 CSV 文件中使用整个字符串作为分隔符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42140543/

相关文章:

javascript - 用变量和 anchor 替换正则表达式

php - 正则表达式:删除字符串末尾的数字和特定字符串后的文本

python - 根据日期列范围将列添加到数据框

python - 如何避免 pandas 在保存的 csv 中创建索引

Python 元类与对象实例化一起使用

python - 删除 Pandas 中的标点符号

python - If/Then 根据数组中的每个值应用不同的函数

java - 匹配字符串的模式

python - 在 Panda Python 中使用偏差和均值函数

python - Django 查询集,小于或大于版本号