CSV quoting rules: commas, double quotes and escaped delimiters
How CSV handles commas, double quotes and line breaks inside fields, why doubled quotes are the common escape, and how to check that a quoted export keeps its meaning.
A comma inside a CSV value is the most common reason a clean-looking export turns into shifted, broken columns. CSV has no separate escape for the delimiter. The usual solution is to wrap the whole field in double quotes. A double quote inside such a field is then written twice. This review explains what that transformation changes, when it is the right choice, and where it can still cause trouble after the file is copied, shared or opened elsewhere.
Short verdict: is CSV quoting suitable for your use?
If the file will be read by software, quoting is the right choice whenever a value contains a comma, a double quote or a line break. The main benefit is that each value comes back exactly as it went in, punctuation included. The tradeoff is that the raw file becomes harder to read, search and edit by hand. It also depends on every program in the chain following the same quoting convention. Tools that disagree on that convention will misread the data without any error message.
Unquoted plain text is the better choice in a few cases. One is when no value contains a delimiter, quote or line break, so quoting would only add noise. Another is when people will mainly read the file in a text editor. A third is when the receiving system only splits on a delimiter and does not understand quotes at all. In that last case, a delimiter that never appears in the data, such as a tab, may be safer than relying on quotes.
What quoting actually changes
CSV quoting changes the stored characters. It is not a display effect. The common convention, described in RFC 4180 and followed by many tools, works like this:
- Fields are separated by commas, and records are separated by line breaks.
- A field that contains a comma, a double quote or a line break must be enclosed in double quotes.
- Inside a quoted field, each literal double quote is written as two double quotes.
- A reader that understands the rule removes the outer quotes and turns each pair of quotes back into one, which restores the original value.
- Fields without special characters may be left unquoted. Quoting them anyway is allowed and does not change their value under this convention.
In CSV, "escaping" usually means doubling a quote. It does not normally mean adding a backslash, and a comma is never escaped by itself. A comma is protected only because it sits inside a quoted field. Some exporters, database formats and custom parsers do use backslash escapes instead, such as a backslash before a quote or before the delimiter. That is a different dialect, not standard CSV. A file written one way and read the other way will be split or decoded incorrectly.
Worked examples
Each row below shows an original value and how the doubled-quote convention stores it. Only the stored form appears in the file.
| Original value | Stored in the CSV file | Why |
| Lee | Lee | No special characters, so no quoting is needed |
| Smith, John | "Smith, John" | Contains the delimiter |
| Said "hello" twice | "Said ""hello"" twice" | Contains double quotes, which are doubled inside the wrapper |
| She said "yes", then left | "She said ""yes"", then left" | Contains both quotes and a comma; one wrapper covers both |
| (empty) | (nothing) or "" | Both forms are commonly read as an empty string, but some tools treat them differently, for example as null versus empty |
Here is a complete small file using these rules. It has three columns, and every row parses into exactly three fields:
id,name,note
1,"Smith, John","Said ""hello"" twice"
2,Lee,plain value
3,Ng,"First line
Second line"
Record 3 spans two physical lines. The line break belongs to the note value because it sits inside quotes. A parser that understands quoting reads it as one record. A tool that simply splits on line breaks will see a fourth, malformed row.
For contrast, this is what happens when the comma in a name is left unquoted:
id,name,note
1,Smith, John,plain value
Row 1 now has four fields instead of three. Depending on the reader, " John" moves into the note column and every value after it shifts right, or the row is rejected. This is the typical failure behind complaints about commas in a CSV file.
Here is the backslash-style dialect, for recognition only:
1,"Said \"hello\" twice"
A reader that expects doubled quotes will treat the quote after the backslash as the end of the field. That leaves stray text and breaks the row. If your source system writes backslash escapes, configure the reader to match, or convert the file to doubled quotes before sharing it.
Details that commonly go wrong
- Spaces around delimiters: under the RFC 4180 convention, spaces are part of the field. The text a, "b" is not a correctly quoted field, because the quote is not the first character. Readers vary in how they handle this.
- Quote at the start of an unquoted value: a value such as "quoted" word (a quoted word followed by more text) must itself be wrapped and doubled, or a reader may mistake its first quote for an opening wrapper.
- Other delimiters: in semicolon- or tab-separated files the same quoting rules usually apply, but a field needs quoting when it contains that file's delimiter. A comma may then need no protection at all.
- Locale-dependent separators: some spreadsheet applications choose a default list separator based on regional settings. A comma-separated export can open as a single column on a machine that expects semicolons. Check this in the specific application and locale you are targeting.
- Record endings: RFC 4180 specifies CRLF line breaks, but many tools write and accept LF only. Mixed endings inside one file can confuse strict parsers.
- Encoding: quoting does not deal with character encoding. Accented letters or other non-ASCII text can still be garbled if the writer and reader assume different encodings, or if a byte order mark is added or expected inconsistently.
Will the result stay usable after copying or sharing?
Opened in a CSV-aware program, a correctly quoted file shows the original values with no visible quotes. Outside that setting, the stored form has practical side effects:
- Copy and paste: copying raw lines from a text editor copies the wrapper and doubled quotes too. Pasting that text into a form field or document gives you the escaped form, not the original value. Pasting into a spreadsheet may or may not trigger CSV parsing, depending on the application and on whether it splits pasted text on tabs or commas.
- Search: searching the raw file for a quoted phrase will often fail, because the file contains doubled quotes. Line-based tools will also miss or split values that contain embedded line breaks.
- Counting and slicing: counting lines or taking the first N lines of a file does not give a reliable record count when quoted fields contain line breaks.
- Hand editing: deleting a single quote, or adding a comma to an unquoted field, silently changes the column structure from that point on. Quote balance is easy to break and hard to spot by eye.
- Reading order and assistive technology: in a raw text view, the extra quotes and commas are read out or displayed as part of the content, which adds noise. When opened as a table, the values may present cleanly. No testing with specific screen readers or applications was available for this article, so check your actual destination before relying on either behavior.
Provide a plain-text or table equivalent if people, rather than programs, are the main audience. Examples are a formatted table in a document, a spreadsheet file, or a short readable summary. Keep the quoted CSV as the machine-readable copy.
How to check that an export preserved its meaning
- Use a real CSV writer, such as your language's CSV library or the application's export function. Building rows by joining strings with commas is the usual source of unquoted delimiters.
- Include test values containing a comma, a double quote, a comma and quote together, a line break, leading and trailing spaces, an empty value and non-ASCII characters.
- Read the file back with the same parser and settings the recipient will use, and compare each value with the original.
- Check that every record has the expected number of fields. A count mismatch is the clearest sign of a quoting problem.
- Confirm which dialect the recipient expects: delimiter, quote character, doubled versus backslash escaping, line ending and encoding. Document it alongside the file.
- Open the file once in each target application, because one successful import does not prove the file works everywhere.
Gaps in this review
This article describes the widely documented quoting convention and general parsing behavior. It does not include measured tests of particular spreadsheet programs, databases, online converters or assistive technologies. It makes no claims about how any specific tool handles privacy or stores uploaded data. Where behavior depends on a named application, locale or reader setting, verify it directly in that environment.