Require complete dictionary rows before exporting CSV
A CSV export with a missing quantity can look like a legitimate empty cell. csv.DictWriter fills absent fields with its default empty value. Validate the row keys explicitly when every column is required.
Set up
Tested on Linux with CPython 3.12.14. Use Bash, Python 3.12 or newer, a text editor, and the mktemp, rm and rmdir utilities. No packages or network access are needed. Keep the same terminal open and stop if setup fails. Run without -O so the assertions remain enabled.
start_dir="$PWD"
work_dir="$(mktemp -d)" || exit 1
cd -- "$work_dir" || exit 1Run the example
Save as strict_csv_export.py:
import csv
from io import StringIO
FIELDS = ("name", "quantity")
EXPECTED = set(FIELDS)
def render_export(rows):
for number, row in enumerate(rows, start=1):
missing = EXPECTED - row.keys()
extra = row.keys() - EXPECTED
if missing or extra:
raise ValueError(
f"row {number}: missing={sorted(missing)}, extra={sorted(extra)}"
)
with StringIO(newline="") as output:
writer = csv.DictWriter(output, fieldnames=FIELDS, lineterminator="\n")
writer.writeheader()
writer.writerows(rows)
return output.getvalue()
with StringIO(newline="") as output:
writer = csv.DictWriter(output, fieldnames=FIELDS, lineterminator="\n")
writer.writerow({"name": "pen"})
print("Default missing field:", repr(output.getvalue()))
for rows in ([{"name": "pen"}], [{"name": "pen", "quantity": 2, "note": "sale"}]):
try:
render_export(rows)
except ValueError as error:
print("Rejected:", error)
result = render_export([{"name": "pen, blue", "quantity": 2}])
assert result == 'name,quantity\n"pen, blue",2\n'
print("Complete export:", repr(result))
assert render_export([]) == "name,quantity\n"Run:
python3 -B strict_csv_export.pyObserved output:
Default missing field: 'pen,\n'
Rejected: row 1: missing=['quantity'], extra=[]
Rejected: row 1: missing=[], extra=['note']
Complete export: 'name,quantity\n"pen, blue",2\n'Validate the complete batch before rendering
The first write demonstrates the default behavior: an absent quantity becomes an empty cell. The export function then rejects both a missing field and an unexpected field. The complete row preserves its comma inside CSV quotes.
This function expects a reusable list of dictionaries with string keys. Its first pass validates every row; its second pass writes them. Passing a one-shot iterator would exhaust it during validation. Materialize a bounded input first if your caller supplies an iterator, or design a separate streaming contract.
The CSV reference documents the missing-field replacement and default error for extra keys. Keeping the explicit check gives both cases the same row-numbered error format.
Keep field presence separate from value validation
An existing key can still contain an empty string, an invalid quantity, or None. This example checks key presence only. Add value checks before rendering when the receiving system requires particular types or nonempty values. CSV conversion can otherwise lose distinctions, including the distinction between None and an empty string.
The result stays in memory until validation and rendering finish. Memory use grows with the export size, so this approach suits small datasets. No destination file is opened here. An application writing the returned text still needs its own failure handling, and should open CSV files with newline="".
Clean up
Remove the saved file and empty scratch directory. If the directory is not empty, inspect its contents before removing anything else.
rm -- strict_csv_export.py
cd -- "$start_dir" || exit 1
rmdir -- "$work_dir"
unset start_dir work_dirAcknowledgements
This post was created with assistance from AI.