Data Reference

Quotes and Line Breaks in CSV

A line break belongs to a CSV field only when it appears inside a double-quoted field; outside quotes it ends the record, so wrap the field in double quotes and double any embedded quotes.

The short answer

A line break is part of a field only when it appears between the opening and closing double quotes of that field. Outside quotes, a line break ends the record. RFC 4180 implements this by allowing a quoted field to contain CR, LF, commas, and doubled double quotes. To keep a break in data, wrap the entire field in double quotes; to keep a literal quote, write two quotes in a row.

A minimal example

id,note
1,"first line
second line"
2,"He said ""hello"""

The first record has two fields, and its second field contains one embedded newline. The second record's note contains literal double quotes, escaped by doubling them.

import csv

with open("notes.csv", newline="", encoding="utf-8") as f:
    rows = list(csv.reader(f))

print(len(rows))    # 3: header plus 2 records
print(rows[1][1])   # first line\nsecond line
print(rows[2][1])   # He said "hello"

The newline="" argument matters: it stops Python's text layer from translating line endings before the CSV parser sees them.

Why this happens

A CSV parser is a small state machine, not a line splitter. It tracks whether it is at the start of a field, inside an unquoted field, inside a quoted field, or just after a quote within a quoted field. In the quoted state, commas and line breaks are data. A "" pair is an escaped quote and remains inside the field. Only the closing quote returns the parser to record syntax.

RFC 4180 states that fields containing line breaks, double quotes, and commas should be enclosed in double quotes, and that a double quote inside a quoted field is escaped by another double quote. It names CRLF as the record separator, but real files use LF, CRLF, or a mixture.

This is why for line in f and line.split(",") fail on valid CSV. They see physical lines, not records. A quoted field spanning three physical lines belongs to one record, not three.

What changes the answer

Variant Effect
LF instead of CRLF Accepted by most parsers, including Python's csv
Semicolon delimiter Same quoting rules; commas become ordinary data
Space before an opening quote The field is not treated as quoted; the quote becomes data or an error
Unclosed quote Parser consumes to end of file; Python joins the remainder by default and raises with strict=True
Blank line Python returns []; it is not a record with one empty field
BOM at start Becomes part of the first header name unless removed

RFC 4180 is informational and does not settle every case. Excel, database export tools, and locale-aware spreadsheet settings produce variations. The quoting rule itself is stable: quoted regions can contain record separators; unquoted regions cannot.

Edge cases

  • The embedded break is preserved as written. A CRLF inside a quoted field stays \r\n unless you normalize it.
  • A line break inside an unquoted field ends the record and usually triggers a field-count error on the next physical line.
  • An unclosed quote is the most common cause of a parser suddenly consuming the rest of a file. Treat end-of-file inside a quote as an error.
  • csv.reader with strict=False accepts some malformed input. Use strict=True when validating untrusted files, and still check field counts.
  • If the first column name looks like \ufeffid, a UTF-8 BOM is present; see recognizing a UTF-8 BOM.
  • A quoted empty string "" yields one empty field, while a blank physical line yields no fields.

Practical checks

  1. Parse with a real CSV library and compare the number of parsed records with the expected logical record count, not with wc -l.
  2. Open files with newline="" in Python so the parser, not the text layer, decides what a line break means.
  3. Round-trip a fixture containing a comma, a double quote, and a CRLF inside a single field.
  4. Assert that every record has the expected number of fields; this catches unclosed quotes and stray delimiters.

Sources

Revision history

Published
Updated
— Initial publication.