Quotes and Newlines Inside CSV Fields

A quoted CSV field can include delimiters, escaped quotation marks, and under common CSV rules an embedded line break.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Read one quoted value
  2. Read a logical row across lines
  3. Count logical records instead of physical lines
  4. Keep source position available during parsing

Read one quoted value

In the standard double-quote convention, "say ""yes""" represents the field text say "yes". A generic quote-removal step would erase the inner quotation marks. See escaping for the difference between framing and data.

Read a logical row across lines

An embedded newline inside quotes may continue the same record. Counting every physical newline as a row boundary can split it incorrectly. Line-ending representations help distinguish the physical file layout from parsed records.[1]

Count logical records instead of physical lines

In the familiar CSV quoting convention, a double quote inside a quoted field is escaped by doubling it. A field can also contain a newline before its closing quote. Passing each physical line as a separate finished record breaks that case. See Line endings in data.

Logical record field 1: said "yes"
Logical record field 2: two lines of notes
Serialized field 1: "said ""yes"""
Field 2 may span two physical lines while quotes stay open

Keep source position available during parsing

Diagnostics can identify both record number and the physical line where it started. Define behavior for a quoted field whose closing quote never arrives. Test a valid embedded newline and a truncated quoted record separately.

References

  1. IETF, RFC 4180: Common Format and MIME Type for CSV Files ↩