如何在pandas read_csv中忽略字段内的quotechar?

时间:2017-10-06 19:17:16

标签: python pandas

我使用pandas read_csv:

pd.read_csv(filepath_or_buffer, sep=None, error_bad_lines=False, skipinitialspace=True)

我有错误行:

Skipping line 818: ',' expected after '"'

导致错误的一行,其中quote是字段内的,但我需要忽略它们:

..."TRANSIT CARGO VAN","T-150 148" LOW RF","...

如果quotechar只围绕分隔符或在行的开头或结尾,而其余的“”将被忽略,问题将会解决,但我不知道如何。

有谁知道如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

我创建了一个这样的文件:

"Valid value","Another valid value","A third valid value"
"Valid value","Another valid value","A third valid value"
"Valid value", "Invalid " value","Invalid line"
"Valid value","Another valid value","A third valid value"

打开它

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

结果是:

             0                    1                    2
0  Valid value  Another valid value  A third valid value
1  Valid value  Another valid value  A third valid value
2  Valid value    "Invalid " value"         Invalid line
3  Valid value  Another valid value  A third valid value

因此,在这种情况下,pandas默认值效果很好。