CSV error reporting: logical record number or physical line range for multiline fields? #212004
Replies: 1 comment
|
I'd recommend reporting both: keep the logical record number and add a start-end physical line range. When the record occupies a single physical line, simply display
Consider this synthetic example: id,note
1,"first entry
2,second entry
3,third entryThe quoted field is never closed, so the reader may consume everything through EOF as part of one record. An error reported at line 4 would send someone to Reporting A second example is a multiline record with an incorrect field count: id,note,tag
1,ok,a
2,"line one
line two",b,extra
3,ok,cHere, the third logical record occupies physical lines 3-4 but contains four fields instead of the expected three. An error such as: is more informative than simply reporting Why I would keep both:
Implementation considerations:
start_line = reader.line_num + 1
try:
row = next(reader)
except StopIteration:
break
end_line = reader.line_num
Overall, I think adding the physical line range is worth the small increase in message length. Keeping the logical record number alongside it provides useful context while making multiline CSV errors significantly easier to debug. |
Uh oh!
There was an error while loading. Please reload this page.
🏷️ Discussion Type
Question
Body
I would like feedback on an error-location design in a small Python standard-library CSV merge tool.
The parser accepts quoted multiline fields. For a field-count error it currently reports the source filename, logical record number (header is record 1), and the physical line where that record ends (
csv.reader.line_num). A generated example produces:{"status": "error", "error": "synthetic-invalid.csv: record 2, line 2: field count mismatch", "synthetic_demo": true}For a multiline record, the physical line can differ from the record number. Would a start/end physical-line range be more useful when fixing an export in an editor, or would it create more confusion than a logical record number? A small synthetic example where the current location would be misleading would help.
The implementation is here, at the published commit; the local demo and format rules give the surrounding context. It validates UTF-8/comma-separated inputs with identical ordered headers and stops on bad input rather than writing a partial CSV. Whitespace is preserved by default; trimming is opt-in. The demo is synthetic, including the four-to-three-record trimming example.
Please use invented rows only, with no customer files, personal data or confidential information.
AI assistance: the code and drafting used Codex (GPT-6). This single post is prepared and submitted by an owner-authorized Codex assistant; the implementation details and demo behavior above were checked against the linked published source and recorded local execution results. The purpose is feedback on this specific reporting tradeoff.
Resubmitted using this category's web form and template after the automated format feedback on the original closed discussion.
Guidelines
All reactions