首页 > 解决方案 > 为什么 Pandas 在读取 csv 文件时忽略 doublequote=True

问题描述

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

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

我正在阅读这个 CSV 文件pandas,并且这一行应该被解释为:

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 容器中将 doublequote 更改为False,那么我会得到同样的错误,但True它可以工作,但在服务器上,它既不工作False也不工作True,看起来 doublequote 什么也没做。

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

标签: pythonpandascsv

解决方案


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

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

也许你可以尝试另一个版本的熊猫。我的是 1.2.5,它按预期工作。


推荐阅读