Explain strict CSV parsing as a choice, not a repair
Also published in our Blogger archive.
Quick answer
The dialect strict setting controls whether csv raises on bad CSV input; it does not validate a business schema.
Example
The unclosed quote is accepted by a non-strict reader as text through end of input, but strict mode raises csv.Error. The script shows both outcomes rather than claiming that either one repairs the file.
import csv, io
bad = 'id,note\n1,"open\n'
loose = list(csv.reader(io.StringIO(bad), strict=False))
try:
list(csv.reader(io.StringIO(bad), strict=True))
except csv.Error:
strict = 'error'
assert loose[1] == ['1', 'open\n'] and strict == 'error'
print('loose=accepted strict=error')
Expected stdout:
loose=accepted strict=error
Reading the result
A strict syntax decision still leaves required headers, allowed values, and duplicate keys to other checks. Keep parser policy and business policy in separately named findings.
Use a malformed quote fixture in regression tests so a future dialect change does not quietly alter the accepted syntax. Keep that fixture distinct from valid quoted newline coverage.
The two parsing results are evidence for a policy choice. Neither result tells an importer whether the note field meets a business rule after syntax has been handled.
Sources
- Python csv module documentation
Prepared with AI assistance. The example uses synthetic data; its stated limits apply.