python data_error ai_generated true

pandas.errors.ParserError: 数据分词错误。C 错误:第 42 行期望 5 个字段,实际出现 7 个

pandas.errors.ParserError: Error tokenizing data. C error: Expected 5 fields in line 42, saw 7

ID: python/pandas-parsererror-tokenizing

其他格式: JSON · Markdown 中文 · English
80%修复率
88%置信度
0证据数
2024-04-03首次发现

版本兼容性

版本状态引入弃用备注
3.x active — — —

根因分析

CSV 某一行的分隔符数量多于表头,通常由于带引号字段内未转义的逗号,或文件中途引入了额外列。

English

A CSV row contains more delimiters than the header row, usually due to unescaped commas inside a quoted field or an extra column introduced mid-file.

generic

解决方案

  1. 90% 成功率
    pd.read_csv(path, on_bad_lines='skip')  # or 'warn'
  2. 85% 成功率
    pd.read_csv(path, sep=';', quoting=csv.QUOTE_ALL) after inspecting line 42

无效尝试

常见但无效的做法:

  1. 90% 失败

    Removed in pandas 2.0; raises TypeError

  2. 70% 失败

    Slower but still raises ParserError on bad rows