Newlines in CSV: records and multiline fields

How CSV separates records with line breaks, how quoted fields can hold embedded newlines, and when converting or flattening those newlines changes your data.

In CSV, a newline can play two different roles. Usually it ends a record. Inside a quoted field, it is part of the data. Most problems with CSV newlines come from mixing up these roles. Line-ending conversions, line-by-line tools and dialect sniffers can split, merge or alter records when they treat every line break as a record boundary. This review explains what each role does, which transformations keep the data intact, and when a flattened plain-text version is the better choice.

Short answer: is a multiline CSV field suitable for your use?

Keep embedded newlines when the CSV moves between parsers that follow the common quoting convention, and when the line breaks carry meaning. Addresses, notes and multi-paragraph descriptions are typical cases. The benefit is that the original text survives the round trip unchanged. The main cost is that the file no longer has one record per physical line, so anything that reads it line by line will miscount or split records.

Plain single-line fields are the better choice in three situations: the consumer is a line-oriented tool or a loader you cannot configure, people will read or edit the file in a basic text editor, or the destination's handling of quoted newlines is unknown and you cannot test it. In those cases, replace embedded newlines with a documented placeholder or a space, and keep the original multiline version as the source of truth.

How CSV uses newlines

The most widely cited description of CSV, RFC 4180, separates records with a line break written as CRLF (carriage return followed by line feed). In practice, many producers and parsers also accept a bare LF. RFC 4180 also says that a field containing a line break, a double quote or the delimiter should be enclosed in double quotes, and that a double quote inside such a field is written as two double quotes. A conforming parser therefore ends a record only at a line break that falls outside quotes. A line break between quotes belongs to the field's value.

The example below has four physical lines but three logical records: a header and two data rows. The second record's note field contains a line break, and the third record's note contains an escaped quote.

id,name,note
1,Ana,"First line
second line"
2,Ben,"He said ""hi"""

A quote-aware parser reads the note in record 1 as two lines of text joined by a line break, and the note in record 2 as: He said "hi". A parser that splits on every line break would instead produce four rows. Two of them would be malformed, each holding half of record 1.

What a transformation changes: stored characters vs. display

Separate the characters stored in the file from how a program displays them. An editor that shows record 1 across two lines changes nothing; that is only presentation. The following operations do change stored characters, and each affects the data differently:

  • Line-ending conversion (LF to CRLF or the reverse) usually rewrites every line break in the file, including those inside quoted fields. Record boundaries survive, but the field values change, for example from LF to CRLF.
  • Flattening replaces embedded newlines with a space, a visible marker or an escape such as a literal backslash-n. The data then fits one record per line, but a consumer cannot recover the original text unless it knows the convention.
  • Removing quotes from fields that contain newlines, delimiters or quote characters breaks record and field boundaries. For such fields this is never a safe cleanup step.
  • Re-exporting from a spreadsheet or another tool may change quoting style, line endings or encoding. Compare a sample of the output with the input before you rely on it.

Newlines and dialect detection

Dialect detection tries to infer the delimiter, quote character and sometimes the line terminator from a sample of the file. Embedded newlines make this harder in two documented or easily checked ways. First, if the sample is cut by a byte or line count, it can end in the middle of a quoted field, and the sniffer then sees an unbalanced quote. Second, a heuristic that compares delimiter counts per line will see uneven counts on the continuation lines of multiline fields. Either effect can lead to a wrong guess. When you know your files contain multiline fields, declare the dialect explicitly rather than relying on detection. If you must use detection, give it a sample that ends at a record boundary.

The line terminator also needs care during detection. A file can legitimately use CRLF between records and bare LF inside fields. A tool that picks the most frequent line ending in the sample may choose the in-field style if long multiline notes dominate the sample.

Reading and writing multiline fields correctly in code

Python's standard csv module is a well-documented example. Its documentation says to open CSV files with newline set to an empty string. Otherwise, newlines inside quoted fields will not be interpreted correctly, and on platforms that use CRLF an extra carriage return can be added when writing. The module then handles quoting and embedded line breaks itself.

import csv

with open("data.csv", newline="", encoding="utf-8") as f:
    for row in csv.reader(f):
        print(row)

The same principle applies in other languages: give the raw file to a CSV parser rather than splitting it into lines first. Splitting on line breaks and then on commas only works for files known to contain no quoted newlines or quoted delimiters.

Copying, sharing and editing the result

  • Line-oriented command-line tools such as line counters, pattern search, head/tail and file splitters work on physical lines. On a file with multiline fields, they report wrong record counts, return fragments of records, or split a record across output files.
  • In plain text editors, a multiline field looks like extra rows. Manual edits made on that basis can easily delete or misplace a closing quote, which merges or breaks later records.
  • Pasting CSV text into a spreadsheet or form may not use the same parser as opening the file directly. Whether quoted newlines survive depends on the specific application and version, and that was not tested for this article.
  • Search inside a multiline field may fail if the search tool matches line by line and the phrase spans the embedded break.

Accessibility depends on the viewer. Once a conforming application has parsed the CSV into a table, the embedded newline is just a line break inside a cell. How a given screen reader announces that break, and whether it reads the cell in order, depends on the application and assistive technology. No documentation or test covering a specific combination was available for this review, so check it in the environment your readers will use. If you cannot check, offer a flattened version alongside the original.

When to provide a plain-text equivalent

Provide a flattened or separately documented version when any of the following applies: the file will go through line-based tooling, the recipient's import process is unknown, people will review the data in a plain text view, or a downstream system has no setting for quoted newlines. Name the replacement convention in accompanying documentation or a header note, so the original line breaks can be reconstructed if needed.

Gaps in this review

No subject-specific measurements, product tests or tool comparisons were available for this article. The guidance draws on the widely published CSV conventions described in RFC 4180, Python's documented csv module behavior, and general properties of line-oriented processing. The way any particular spreadsheet, database loader, sniffer or assistive technology handles embedded newlines should be confirmed against its own documentation or with a small test file, like the example above, before you rely on it.