【问题标题】:How can I ignore quotechar inside field in pandas read_csv?如何忽略 pandas read_csv 中字段内的 quotechar?
【发布时间】:2017-10-06 19:17:16
【问题描述】:

我使用熊猫 read_csv:

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

我有错误行:

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

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

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

如果 quotechar 将仅在分隔符周围或在行首或行尾,而 '"' 的其余部分将被忽略,则问题将得到解决,但我不知道如何解决。

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

【问题讨论】:

  • 默认是sep=',',所以你不需要包含那个参数
  • @ScottBoston 我使用 None 因为有些文件有不同的分隔符 If sep is None, the C engine cannot automatically detect the separator。我无法替换它们,因为我在 6,001 - 10,000 中也有分隔符。

标签: python pandas


【解决方案1】:

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

"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 默认值在这种情况下效果很好。

【讨论】:

    猜你喜欢
    • 2016-09-01
    • 2014-07-27
    • 2021-08-22
    • 2019-01-31
    • 2021-08-06
    • 1970-01-01
    • 1970-01-01
    • 2017-05-01
    • 2021-10-03
    相关资源
    最近更新 更多