CParserError: Error tokenizing data (now raised as pandas.errors.ParserError: Error tokenizing data in modern Pandas) occurs when Pandas’ fast C CSV engine encounters a row with more or fewer columns (delimiters) than expected based on the header row, or hits an unclosed quote character (") that breaks row boundaries. Fix it by specifying the actual file delimiter (sep=";" or sep="\t"), handling malformed lines with on_bad_lines="skip" (or "warn"), or switching to engine="python" when complex multi-character separators or irregular quoting are present.
Basic Example
import pandas as pd
# 1. FIX A: Wrong delimiter (e.g., semicolon- or tab-separated file read with default ',')
df = pd.read_csv("data.csv", sep=";")
# 2. FIX B: Skip or warn on corrupted rows that have extra unquoted commas
df_clean = pd.read_csv("messy_logs.csv", on_bad_lines="warn") # Options: 'error', 'warn', 'skip'
# 3. FIX C: Unbalanced quotes or complex delimiters -> use the flexible Python parser engine
df_python = pd.read_csv("unquoted_text.csv", engine="python", on_bad_lines="skip")
Key Details & Root Causes
- 1. Mismatched delimiter (
sep/delimiter): By default,pd.read_csv()splits on commas (,). If your file uses semicolons (;), tabs (\t), or pipes (|), Pandas reads the entire header as a single column—and then crashes on the first data row that happens to contain a comma inside a text field (Expected 1 fields in line 2, saw 2). Passsep=";"(orsep=None, engine="python"to auto-detect the delimiter). - 2. Unescaped delimiters inside text fields: If row 1 (the header) has 4 columns, and row 15 contains unquoted text with an extra comma (e.g.,
101,Alice,"Engineer",Lahore, Pakistan), the C parser sees 5 fields instead of 4 (Expected 4 fields in line 15, saw 5).- Fix: Use
on_bad_lines="skip"(Pandas 1.3+, replacing the deprecatederror_bad_lines=False) to discard malformed rows, or pass a custom callable toon_bad_lines=lambda bad_line: ...to inspect/repair broken rows on the fly.
- Fix: Use
- 3. Metadata/preamble lines above the actual header: Exporting CSVs from Excel, financial tools, or sensors often adds 2–3 title lines at the top of the file before the real column headers begin. Skip them cleanly using
skiprows=3(orheader=3). - 4. Unbalanced or stray quote characters (
"): A stray double-quote inside a product description (such as24" Monitor) causes the C parser to treat subsequent newlines and commas as part of one giant multi-line string until its internal buffer overflows. Passquoting=csv.QUOTE_NONE(alongsideescapechar="\\") if quotes in the file are literal characters rather than field wrappers. - 5. Legacy Pandas import path note: In Pandas < 0.20, this exception was located at
pandas.parser.CParserError; in Pandas 0.20+, it was renamed and moved topandas.errors.ParserError(which inherits fromValueError).
Diagnostic & Fix Reference
| Error Message / Symptom | Root Cause | Solution in pd.read_csv() |
|---|---|---|
Expected 1 fields in line 2, saw N |
File uses ;, \t, or | instead of , |
Specify the real delimiter: sep=";" or sep="\t" (or sep=None, engine="python"). |
Expected N fields in line X, saw N+1 |
Extra unquoted commas inside a specific row | Pass on_bad_lines="skip" or on_bad_lines="warn" to bypass malformed rows. |
Expected 1 fields in line 4, saw 8 |
1–3 title/metadata rows precede the header | Skip preamble lines using skiprows=3 (or set header=3). |
EOF inside string starting at row X |
Stray unclosed " quote character in text |
Pass quoting=3 (csv.QUOTE_NONE) or engine="python". |
Buffer overflow / huge multi-line fields |
Complex regex separators or irregular quoting | Switch from C engine to Python engine: engine="python". |
You must log in or register to comment.
