Skip to content
Multi-Line Fields
step 1/5

Reading — step 1 of 5

Read

~1 min readQuoting

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.

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…