python - 以 $$ 作为分隔符在 pandas 中导入 CSV

标签 python csv pandas

我正在尝试使用 $$ 作为分隔符在 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/

相关文章:

python - py2exe,可执行文件 : How to pack data into single file?

python - 如何在 Django 中排除表单中的继承字段?

java - Jython 中导入的类中缺少属性

c# - 如何使用 C# 将 CSV 数据粘贴到 Windows 剪贴板

ms-access - Access : Truncation error when appending CSV data to tables?

python - 将 lambda 函数应用于具有 NaN 值的数据框?

python - 在Python中比较pandas数据帧的单列值(将perl转换为python代码)

python - 覆盖类的方法定义

python - scrapy CSV编写

python-3.x - 没有名为 'pandas' 的模块 - Jupyter、Python3 内核、TensorFlow 通过 Docker