DEV Community

SUAD CUMA
SUAD CUMA

Posted on

Catch CSV row-width drift before it breaks an import

Catch CSV row-width drift before it breaks an import

A spreadsheet export can look ordinary but break a bulk import when a line has fewer or more columns than the header. This is a structural problem, separate from missing values or duplicate business identifiers.

Consider this entirely synthetic sample:

order_id,amount,currency
1001,29.50,USD
1002,19.00
1003,"39,00",EUR,EXTRA
1004,10.00,USD
Enter fullscreen mode Exit fullscreen mode

Line 3 has only two fields. Line 4 has four. The comma inside the quoted amount 39,00 belongs to a single field, and the extra field is a separate problem.

Reproducible validation with standard-library Python

import csv
from io import StringIO

sample = """order_id,amount,currency
1001,29.50,USD
1002,19.00
1003,"39,00",EUR,EXTRA
1004,10.00,USD
"""

reader = csv.reader(StringIO(sample), strict=True)
header = next(reader)
width_errors = [
    (line_number, len(fields))
    for line_number, fields in enumerate(reader, start=2)
    if len(fields) != len(header)
]

print("Expected columns:", len(header))
print("Width errors (line, columns):", width_errors)
Enter fullscreen mode Exit fullscreen mode

Expected output:

Expected columns: 3
Width errors (line, columns): [(3, 2), (4, 4)]
Enter fullscreen mode Exit fullscreen mode

What the test does—and what it cannot do

The check counts parsed CSV fields, not literal comma characters. Python's CSV parser respects quoted commas, so do not split a CSV record manually on commas. Row-width validation is useful before trying to map exports to database columns.

Rows with correct field counts may still contain invalid amounts, duplicate business keys, empty required values, or unsupported currency codes. Those are separate data-quality controls. When testing a real import, preserve the original file, confirm encoding and delimiter settings, and check the target importer's mapping before writing production data.

This example uses fabricated data and accesses no client account. You can also explore the free browser-local CSV audit on a small test file.


Disclosure: This technical tutorial was prepared with AI assistance. The Python example was run with synthetic input, and its output was checked before approval for publication.

Top comments (0)