我正在尝试使用 $$
作为分隔符在 pandas 中导入 csv,我希望以下命令能够工作:
pd.read_csv('data.csv', delimiter="$$")
然而,这会返回以下错误:
Falling back to the 'python' engine because the 'c' engine does not support regex separators (separators > 1 char and different from '\s+' are interpreted as regex), but this causes 'error_bad_lines' to be ignored as it is not supported by the 'python' engine.
这个操作甚至可以在 Pandas 中实现吗?
最佳答案
你可以通过\
转义$
:
df = pd.read_csv('data.csv', sep="\$\$", engine='python')
示例:
import pandas as pd
from pandas.compat import StringIO
temp=u"""a$$b
a$$1
s$$2
f$$3"""
#after testing replace 'StringIO(temp)' to 'filename.csv'
df = pd.read_csv(StringIO(temp), sep="\$\$", engine='python')
print (df)
a b
0 a 1
1 s 2
2 f 3
关于python - 以 $$ 作为分隔符在 pandas 中导入 CSV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44585198/