How to Escape a Comma in CSV
To keep a comma inside a CSV value, wrap the field in double quotes and double any quotes inside it. Rules, examples, a step-by-step method and ways to verify.
In a CSV file, a comma marks the end of one field and the start of the next. To keep a comma inside a value, enclose the whole value in double quotes. If the value also contains double quote characters, write each of them twice. Under the widely followed CSV convention described in RFC 4180, that is the complete escaping rule. There is no separate escape character for the comma itself.
What escaping does to your text
Escaping works on individual field values, such as a name, an address or a note, and not on the whole line. The result is a CSV field that a parser reads back as exactly the original value. The text you care about stays the same. Only the stored form gains extra quote characters, and a CSV reader removes them when it loads the file. Values that contain no comma, double quote or line break don't need any change.
Why an unescaped comma breaks a row
Example of a broken file:
id,name,age
1,Smith, John,42
The header defines three fields, but the second row splits into four: 1, Smith, " John" (with a leading space) and 42. Depending on the program, the row may be rejected, the columns may shift, or the extra value may be silently dropped. The fixed row is: 1,"Smith, John",42
The quoting rules
- A field that contains a comma, a double quote (") or a line break must be enclosed in double quotes.
- Inside a quoted field, write each double quote character as two: " becomes "".
- The opening quote must be the first character of the field, directly after the previous comma or at the start of the line. The closing quote must be followed directly by a comma or the end of the line.
- Fields without these characters may stay unquoted. Quoting them anyway is also valid.
- A backslash is not an escape character under this convention. The sequence , is read as a literal backslash followed by a field separator.
Examples: original value and CSV field
| Original value | Written in the CSV file |
| Smith, John | "Smith, John" |
| 5" screen | "5"" screen" |
| He said "hi", then left | "He said ""hi"", then left" |
| "quoted" | """quoted""" |
| plain text | plain text (quotes optional) |
| (empty value) | nothing between the commas, or "" |
A value that contains a line break is also wrapped in quotes, and the line break stays inside the field. Example of a row whose second field spans two lines:
id,address,city
7,"12 Main St
Apt 4",Springfield
Step by step: escaping values by hand
- 1. Check each value for a comma, a double quote or a line break (carriage return or line feed).
- 2. In those values, replace every " with "". Do this before adding the surrounding quotes. If you do it after, the wrapping quotes get doubled too.
- 3. Enclose the value in a pair of double quotes.
- 4. Join the fields of a row with commas and don't add spaces around them, because spaces become part of the field.
- 5. End each row with a line break, and save the file in the text encoding the receiving program expects. UTF-8 is a common choice, but confirm what your target program requires.
Let a CSV library do the escaping
Escaping by hand is easy to get wrong in large files, and most programming languages include a CSV writer that handles it. The illustrative example below uses Python's standard csv module. According to its documentation, the default writer quotes only fields that need it and doubles embedded quote characters.
import csv
rows = [
["name", "note"],
["Smith, John", 'Said "hi"'],
]
with open("out.csv", "w", newline="", encoding="utf-8") as f:
csv.writer(f).writerows(rows)
Based on those documented defaults, the file should contain the lines below. This is expected output and was not produced by running the code for this guide.
name,note
"Smith, John","Said ""hi"""
Verify the result
- Read the file back with a CSV parser and confirm that every row has the same number of fields as the header.
- Compare the parsed values with your originals. They should match character for character, with no leftover quotes and no lost commas.
- Open the file in a spreadsheet program. A value like Smith, John should appear in a single cell without visible quote marks.
- Look for warning signs in the raw text: a quote in the middle of an unquoted field, or a quoted field with an odd number of quote characters inside it.
Illustrative read-back check in Python, which prints the field count and parsed values for each row:
import csv
with open("out.csv", newline="", encoding="utf-8") as f:
for row in csv.reader(f):
print(len(row), row)
Common pitfalls
- Space before the opening quote. In 1, "Smith, John" the field begins with a space, so a strict parser does not treat the quote as an opening quote and may split the value at the comma. Some parsers have an option to skip leading spaces, so results differ between programs. Remove the space.
- Curly quotes. Typographic quotes such as “ and ” (U+201C and U+201D) are not CSV quote characters. Only the straight double quote " (U+0022) works. Text pasted from word processors often contains curly quotes.
- Backslash escaping. Writing , or " is not part of the common convention. Some programs and libraries offer it as an option, so use it only when the receiving program's documentation says it expects it.
- Other delimiters. Some files use semicolons or tabs instead of commas. The same quoting principle generally applies, but the character that needs protecting is whichever delimiter the file actually uses.
- Opening and re-saving in a spreadsheet. A spreadsheet program may reformat values when it saves, for example by removing leading zeros or changing number formats. Check the saved file if exact text matters.
Limits of this guide
This guide describes the general CSV quoting convention. It does not document how any particular spreadsheet, database or import tool behaves, and none was tested for it. Programs differ in their default delimiter, quote character, encoding and handling of malformed rows. Check the settings or documentation of the program that will read your file.