Step 1 of 5 · Reading · ~1 min
Read
Quoting
Multi-Line Fields
A quoted field can span multiple lines:
name,bio
Alice,"hi I'm Alice
I work in NYC"
Bob,"single line"
The parser must NOT treat a newline inside quotes as a row break. State machine:
states: NORMAL, QUOTED, QUOTED_QUOTE_PENDING
NORMAL: reading unquoted field
',' → emit field, NORMAL
'\n' → emit field, emit row, NORMAL
'"' → start QUOTED state (only if at field start)
QUOTED: reading inside quotes
'"' → QUOTED_QUOTE_PENDING (might be end-of-field or escape)
any other → append
QUOTED_QUOTE_PENDING: just saw " inside quoted; check next
'"' → append literal ", back to QUOTED
',' → emit field, NORMAL
'\n' → emit field, emit row, NORMAL
Implementation: read character-by-character (not line-by-line). Most parsers iterate for c in input and dispatch on state.
This is why csv libraries are preferred over string.split(): the multi-line case routinely trips up DIY parsers.
Up nextEdge CasesEdge Cases
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…