【问题标题】:Why is Pandas ignoring doublequote=True when reading a csv file为什么 Pandas 在读取 csv 文件时忽略 doublequote=True
【发布时间】:2021-08-08 04:59:04
【问题描述】:

我有一个包含如下数据的 CSV 文件:

123,"this is text",123,123,123,"""This is text, it is quoted and has a comma"""

我正在使用pandas 读取此 CSV 文件,此行应解释为:

col1 |     col2     | col3 | col4 | col5 | col6
123  | this is text | 123  | 123  | 123  | "This is text, it is quoted and has a comma"

但是,我不断得到

预期 N 列,但在行号中找到 N+1 ....

来自pandas 的错误。

我在本地机器的 docker 容器中测试了这个,与服务器中相同的 python 和 pandas 版本,在我的机器上它工作正常,但在服务器上却不行。

这是我在两种情况下都使用的命令:

df = pandas.read_csv(csv_path, dtype=str, sep=',', keep_default_na=True, quotechar='"', doublequote=True)

如果我在本地 docker 容器中将双引号更改为 False,那么我会遇到同样的错误,但使用 True 它可以工作,但在服务器上,它既不能使用 False 也不能使用 True,它会似乎双引号什么也没做。

我可以看到的一个区别是 GCC python 使用,在服务器上它说 GCC 4.8.5,在我的本地 docker 容器上它说 GCC 8.3.0,这可能是问题吗?如果是,如何在服务器上指定?

【问题讨论】:

  • GCC 版本几乎不相关。 C 没有任何 CSV 解析函数。
  • 除非您重新编译 pandas,否则您的 GCC 版本无关紧要。
  • 呃,原来这与 pandas 无关,之前有一个组件错误地转换了双引号,它违反了 CSV 标准 -_-

标签: python pandas csv


【解决方案1】:

我刚刚使用您提供的信息创建了两个文件(一个 .csv 和一个 .txt 文件)。两种方法都适合我。

pd.read_csv('file.csv', header=None)
pd.read_csv('file.txt', header=None)

也许您可以尝试其他版本的 pandas。我的是 1.2.5,它按预期工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-28
    • 1970-01-01
    • 2020-11-03
    • 1970-01-01
    相关资源
    最近更新 更多