Batu Lab NotesPractical developer guides

Treat whitespace-only CSV values as missing without rewriting data

By Batu ยท English technical notes

Also published in our Blogger archive.

Quick answer

DictReader supplies strings for named cells, including strings that contain only spaces.

Example

The predicate calls strip() only while deciding whether email is required. It never assigns the stripped value back into the row, so the validator can report the exact export value without silently cleaning it.

import csv, io
rows = list(csv.DictReader(io.StringIO('id,email\n1,   \n2, a@example.test \n')))
missing = [r['id'] for r in rows if not r['email'].strip()]
assert missing == ['1']
print('missing:', missing)

Expected stdout:

missing: ['1']

Reading the result

This rule says nothing about whether surrounding whitespace is allowed in a real email field. Add a separate format rule only after the import contract defines that syntax.

For a nullable optional field, use a different finding kind than required-blank. That keeps an intentional empty value from being conflated with a mandatory value that was lost during export.

Keep the row value unchanged in any output that supports correction. A reviewer needs to see whether the producer sent an empty cell or whitespace that merely looked populated.

Sources

- Python csv module documentation

Prepared with AI assistance. The example uses synthetic data; its stated limits apply.