Batu Lab NotesPractical developer guides

Keep comment-like rows out of a CSV contract discussion

By Batu ยท English technical notes

Also published in our Blogger archive.

Quick answer

The csv module has no comment-row feature, so a hash-prefixed preamble is parsed as an ordinary one-field record.

Example

The fixture first shows the raw first record, then applies an explicit startswith("#") preprocessing rule and shows the resulting header. Both outcomes are visible, which forces the format decision into the contract.

import csv, io
text = '# exported by tool\nid,name\n1,A\n'
raw = list(csv.reader(io.StringIO(text)))
filtered = list(csv.reader(io.StringIO('\n'.join((x for x in text.splitlines() if not x.startswith('#'))))))
assert raw[0] == ['# exported by tool'] and filtered[0] == ['id', 'name']
print('raw-first=# exported by tool filtered-first=id')

Expected stdout:

raw-first=# exported by tool filtered-first=id

Reading the result

Do not copy this filter into a CSV where a real first field may begin with #. A comment convention belongs to the producer and should be versioned with the format.

If comments are allowed, apply the preamble rule before choosing the header. Applying it after header selection can make a comment row accidentally become the schema for all later records.

The filter is applied to raw physical lines in this toy format. That approach should not be used when quoted CSV data itself can contain multiline comment-looking text.

Sources

- Python csv module documentation

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