CSV Rows Have Different Column Counts: Find and Fix the Cause
An extra field can move a price into a description column; a missing field can make the remaining values look valid under the wrong headings. Count parsed fields before trusting the table.
Count fields after parsing, not commas in the line
The schema here is sku, description, price: three fields. A17 contains an additional comma inside a quoted description, so counting punctuation would incorrectly label it as too wide. D40 contains an empty middle field, which still occupies the description position.
A real mismatch appears on B28, where the final comma introduces a fourth, empty field. C39 supplies only two fields. These are separate defects, and neither one tells us whether the missing or extra content was caused by a bad export, a manual edit, or a different schema.
Locate the first mismatch with a repeatable check
Save the sample as input.csv and run the Python script below from the same folder. It reports record numbers including the header. Python strict parsing can catch some malformed quoting, while the explicit length check handles unequal field counts. A successful parse is not proof that values match the intended business columns.
import csv
with open("input.csv", encoding="utf-8-sig", newline="") as f:
reader = csv.reader(f, delimiter=",", strict=True)
header = next(reader, None)
if header is None:
raise ValueError("The file is empty")
expected = len(header)
for record, fields in enumerate(reader, start=2):
if len(fields) != expected:
print(f"record {record}: {len(fields)} fields; expected {expected}")
# Output for this sample:
# record 3: 4 fields; expected 3
# record 4: 2 fields; expected 3Choose a repair using the source meaning
Check the exporter definition, the corresponding source record, or the file supplier before editing. For this exercise, assume the supplier confirms that B28 ends with an accidental delimiter and C39 has no recorded price. Those confirmations make the changes below justified; the field counts alone would not.
| Case | Meaning | Repair decision |
|---|---|---|
| A17: 3 fields | Comma inside description | Keep unchanged |
| B28: 4 fields | Confirmed extra terminal delimiter | Remove that final delimiter |
| C39: 2 fields | Confirmed missing final price | Add an empty price position |
| D40: 3 fields | Empty description | Keep unless a description is required |
| Unknown middle value missing | Position cannot be inferred safely | Retrieve the original record |
Related help: Check commas and double quotes before editing a record
Write an explicit corrected example
The corrected C39 record ends in a comma because its final price field is empty. That differs from the unwanted fourth field on B28. The same punctuation can therefore be necessary in one record and erroneous in another, depending on the schema.
An empty price is still missing information. Do not substitute zero unless zero is the correct recorded price. A structurally valid table can remain incomplete for invoicing, inventory valuation, or another downstream task.
sku,description,price
A17,"Cup, blue",12.50
B28,Plate,8.00
C39,Spoon,
D40,,4.25Recheck the whole file after fixing one record
Run the field-count check again and confirm four data records remain. Compare the corrected values against the source, then verify required columns separately. In this example, prices present in the source total 24.75, and one price remains missing. Report both facts rather than presenting 24.75 as a complete valuation.
If many rows fail in the same way, investigate the export mapping instead of editing hundreds of lines. If the first failure contains an opening quote, inspect the following physical lines as well: an unfinished quoted field can cause a parser to consume later content as part of the same record.
Do not hide extra fields during import
Check import settings that force a fixed number of columns. A preview can look rectangular after an importer discards extra fields or fills missing positions. Microsoft documents this possibility for Csv.Document when a column count is supplied. Validate the source before such a transformation masks the evidence.
Column Harbor flags selected CSV structural risks; it does not know which business field was omitted or repair the source for you. Keep a short edit log with the affected record, the correction, and how you confirmed it.
Common questions
Are empty cells always column-count errors?
No. A17,,12.50 contains three fields. A17,12.50 contains two and may also put the price under the wrong header. Empty values need separate completeness checks.
Can I fix the file by adding commas to every short row?
Only when you have established which trailing fields are absent. A missing middle field requires a delimiter at that position; padding the end would preserve the misalignment.
Why does one parser reject a file another accepts?
CSV readers have different strictness and field-count settings. Go, for example, exposes an expected-fields setting. Agreement on the schema and dialect matters more than acceptance by one program.
Sources & method
- RFC Editor: RFC 4180, Common Format and MIME Type for CSV Files ↗
- Python documentation: CSV File Reading and Writing ↗
- Go documentation: encoding/csv ↗
- Microsoft Learn: Csv.Document ↗
Examples and diagrams use synthetic data. Application instructions follow the linked documentation; available menus and options can vary. Our sample checks do not establish behavior in every Excel or Google Sheets version. Read our AI-assisted editorial method.