python data_error ai_generated true

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

ID: python/pandas-parsererror-tokenizing

Also available as: JSON · Markdown · 中文
80%Fix Rate
88%Confidence
0Evidence
2024-04-03First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.x active — — —

Root Cause

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

中文

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

Workarounds

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

Dead Ends

Common approaches that don't work:

  1. 90% fail

    Removed in pandas 2.0; raises TypeError

  2. 70% fail

    Slower but still raises ParserError on bad rows