Reading — step 1 of 5
Read
Edge Cases
The cases that break naive parsers:
Trailing comma: a,b, → 3 fields: a, b, ``. The final empty field is significant.
BOM (Byte Order Mark): Excel sometimes prepends \xEF\xBB\xBF to UTF-8 CSV files. Strip on first read.
CRLF vs LF: Mac, Windows, Linux all differ historically. Modern parsers accept any.
Empty line: most parsers either skip or emit an empty row. Configurable.
Leading whitespace: Alice, 30, NYC — should age be 30 or 30? Per RFC 4180, whitespace is significant. In practice, trim by default.
Unbalanced quotes: Alice,"unclosed. Most strict parsers error; lenient ones treat the rest of input as the field's content.
Unicode: csv files in UTF-8, UTF-16, or some legacy encoding. Use encoding='utf-8-sig' in Python to handle BOM.
Headers: not in spec; conventional. Some parsers auto-detect; others require explicit headers=True.
Type coercion: CSV is all strings. The parser doesn't know 30 is a number — that's the application's job.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…