Handle line breaks in CSV fields

How line breaks inside CSV fields work, when to keep, escape or flatten them, and how each choice affects parsing, copying, searching and sharing.

A line break inside a CSV field is legal, but it trips up many tools. Addresses, notes, product descriptions and survey comments often contain multi-line text. When that text lands in a CSV file, the result either survives intact or splits into broken records, depending on how it was written and how it is read. This review covers the three main ways to handle embedded line breaks, what each one changes in the data, and how to choose.

Short answer: which approach suits which use

If the file goes to a CSV-aware parser, such as a spreadsheet import, a database loader or a programming library, keep the line breaks and wrap the field in double quotes. This preserves the original text exactly. The cost is that any tool that treats each physical line as a record will misread the file.

If the file has to pass through line-oriented tools, or will be read in a context that shows one line per record, use one of the other two approaches. Replace each break with an escape sequence such as a literal backslash-n if you need to restore the text later. Replace it with a space or a visible separator if a single line of readable text matters more than an exact round trip.

Plain single-line text is preferable when the receiving system's handling of multi-line fields is unknown and cannot be tested, when people will read or edit the file in a basic text editor, or when the line breaks carry no meaning. Hard-wrapped text pasted from email is a common example of breaks that carry no meaning.

What each approach changes in the stored data

A line break is not formatting. It is one or more stored characters: line feed (U+000A), carriage return (U+000D), or the two together as CRLF. A spreadsheet cell that shows wrapped lines may also be wrapping long text visually with no break character present. Only actual break characters create the CSV problem described here. Visual cell wrapping does not affect the exported data.

Quoting: the content is unchanged

RFC 4180, the widely referenced description of the CSV format, allows a field that contains line breaks, commas or double quotes as long as the field is enclosed in double quotes. A double quote inside such a field is written as two double quotes. Quoting adds wrapper characters to the file, but a conforming parser removes them, so the field value it returns matches the original text, including the break characters.

id,name,note
1,Ada,"First line
second line"
2,Bo,"She said ""hi"""
3,Cy,plain note

The sample above contains a header and three records spread over five physical lines. Record 1's note field holds a line feed, so the record spans two lines. Record 2 shows the doubled-quote rule: a parser reads the note as She said "hi" with single quote marks. Record 3 needs no quoting. A parser that follows the quoting rules returns four rows. A tool that splits on newlines returns five rows, and two of them are fragments.

Escaping: the content changes, but reversibly

Escaping replaces each break character with a marker, usually the two characters backslash and n, so every record fits on one physical line. The stored value is now different from the original, and every consumer must know to decode it. To keep the conversion reversible, also escape any backslashes already in the data. Otherwise a genuine backslash followed by n in the source cannot be told apart from an encoded break. Carriage returns need their own decision: escape them separately, or normalize CRLF to LF first.

Flattening: the content changes permanently

Flattening replaces breaks with a space, a semicolon, a pipe or another separator. The output is the easiest to read on a single line, but the original structure is gone. A paragraph break and a wrapped line become identical, and if the chosen separator already appears in the text, nobody can reliably tell which separators were once breaks. Use this only when the breaks are not meaningful or when the output is read-only.

Approach | Stored text changed? | Reversible? | Safe for line-based tools? | Best fit
Quote the field | No (wrapper only) | Yes | No | Spreadsheets, databases, CSV libraries
Escape as \n | Yes | Yes, if backslashes are also escaped | Yes | Pipelines that pass through grep, split or similar tools
Replace with space or separator | Yes | No | Yes | Read-only reports, single-line previews, search indexes

Line endings and TSV conversion

RFC 4180 specifies CRLF between records, but many producers write LF only, and many parsers accept either. Inside a quoted field, the original break characters are normally kept as they are. As a result, one file can contain CRLF record separators and LF breaks inside fields, or the reverse. This is usually harmless for a proper parser. It can still surprise someone who compares files byte by byte or normalizes line endings with a separate tool after export. That kind of normalization rewrites breaks inside fields as well as between records.

When converting CSV to TSV, check what the target expects. The commonly cited TSV definition does not allow tabs or line breaks inside fields. Many TSV-producing tools therefore escape them, often as backslash-t and backslash-n, instead of quoting. Some tools quote TSV fields the way CSV does. A converter that copies quoted multi-line CSV fields into TSV unchanged may produce a file the destination cannot parse. Confirm the destination's documented TSV rules before choosing.

Reading and writing multi-line fields correctly in code

Most CSV libraries handle quoted line breaks if they are allowed to manage newlines themselves. Python's documentation, for example, says to open CSV files with newline set to an empty string so the csv module can interpret line breaks inside quoted fields. Its default writer quotes any field containing the delimiter, the quote character or a line-ending character.

import csv

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

print(len(rows))  # header + records, not physical lines

with open("output.csv", "w", newline="", encoding="utf-8") as f:
    csv.writer(f).writerows(rows)

The common failure is reading the file line by line and splitting each line on commas. That approach cannot work with quoted multi-line fields, however carefully the comma splitting is done.

Readability and usability after copying or sharing

  • Line-oriented commands count physical lines, not records. Line counts, "show the first N lines" and splitting a file into chunks by line can all cut a quoted record in half.
  • Text search can miss a phrase whose words fall on either side of an embedded break. A flattened copy is easier to search. Escaped text puts a backslash sequence where the space would be.
  • Opening the raw file in a plain text editor shows quoted multi-line records as several lines. This is valid, but editing by hand risks breaking the quote pairing.
  • Copying a multi-line cell out of a spreadsheet and pasting it elsewhere depends on the application and platform. The pasted result may keep the breaks, add surrounding quotes, or split across several cells or rows. Test the specific source and destination pair before relying on it.
  • Spreadsheet imports usually honor quoted breaks when the file is opened through a proper CSV import. Behavior can still differ between applications, versions and import settings, so check the destination's own documentation.

Assistive technology does not read raw CSV files directly in most workflows. What matters is how the destination presents the data. In a spreadsheet, a cell containing a break is still one cell. In an escaped or flattened export, a screen reader may announce a literal backslash-n or a separator character where a pause was intended. If the shared output will be read aloud or reviewed by people using assistive technology, a flattened version with natural punctuation, such as a period or semicolon in place of each break, is likely to read more cleanly than escape sequences. This should be confirmed with the specific reader and application involved.

A quick check before you rely on the output

  • Count records with a CSV parser and compare the number with the source system's row count. Do not compare against a line count.
  • Find at least one field known to contain a break and confirm it arrives as one field with the break intact, or correctly escaped or flattened.
  • Include a field that contains both a double quote and a break, since that combination exposes quoting errors.
  • For escaped output, decode it and compare it with the original to confirm the round trip is exact.
  • Open the result in the actual destination application rather than assuming one successful import means every destination will work.

Gaps in this review

No specific converter, spreadsheet application, version or screen reader was tested for this article, and no measurements are reported. The behavior described comes from the CSV format rules and general parser conventions. Particular tools may differ. The privacy and data-retention practices of online CSV converters were not assessed. Treat any web-based tool as unverified until its own documentation says how uploaded data is handled, and avoid sending sensitive records to one without that information.