Distinguish empty strings from missing CSV columns
Also published in our Blogger archive.
Quick answer
An empty CSV field is represented by an empty string, whereas a short record has fewer list elements.
Example
The first data row contains a present second cell whose value is "". The second lacks that position entirely, so its length is lower than the header length. The two assertions preserve the structural distinction.
import csv, io
rows = list(csv.reader(io.StringIO('id,name\n1,\n2\n')))
empty = rows[1][1] == ''
short = len(rows[2]) < len(rows[0])
assert empty and short
print('empty-field=True missing-column=True')
Expected stdout:
empty-field=True missing-column=True
Reading the result
A reader that fills missing cells with defaults can be useful after validation, but it should not replace this evidence-producing preflight check.
When later mapping a short row, do not index it before emitting its structural finding. An IndexError is less useful than a report that names the expected and actual field counts.
This distinction is useful for remediation: an empty field may need a source value, while a short record can indicate a broken delimiter or truncated row.
Sources
- Python csv module documentation
Prepared with AI assistance. The example uses synthetic data; its stated limits apply.