CSV Quoting Rules From RFC 4180: Commas, Quotes, and Line Breaks
Wrap any CSV field that contains a comma, a double quote or a line break in double quotes, and write each double quote inside it twice. That's the whole idea behind CSV quoting, and it comes straight from RFC 4180. Below you'll find the rules, the edge cases, and the places where real programs disagree.
The RFC 4180 quoting rules
RFC 4180 says: "Fields containing line breaks (CRLF), double quotes, and commas should be enclosed in double-quotes." Quoting is otherwise optional. Each field may or may not be enclosed in double quotes, but if a field is not enclosed, double quotes may not appear inside it.
Escaping a double quote by doubling it
Once a field is quoted, a double quote inside it is escaped by preceding it with another double quote. The RFC's own example is:
"aaa","b""bb","ccc"The middle field is read as b"bb. Wikipedia's CSV article gives a similar one: "Venture ""Extended Edition""" parses to Venture "Extended Edition". Note the three quotes at the end: two for the escaped quote, one to close the field.
Other rules worth knowing
Quoting works alongside a few structural rules:
- Each record is on its own line, delimited by a line break (CRLF).
- The last record may or may not end with a line break.
- An optional header line comes first, in the same format as the records.
- Each line should contain the same number of fields.
- Spaces are part of a field and should not be ignored.
- The last field in a record must not be followed by a comma.
The point about spaces matters when you edit by hand. a, b is a field a and a field b with a leading space, not two tidy values.
Line breaks inside a field
A quoted field may span several lines. The RFC's grammar allows CR and LF inside an escaped field, so this is one record with two fields:
name,note
"Ada","Call back
after lunch"The note value contains a line break. Drop the quotes and the second line looks like a new record with a single field. This is also why line breaks behave differently in plain CSV than in Markdown; if that distinction trips you up, see why line breaks behave differently in Markdown.
The RFC also warns that its MIME registration "uses CRLF to denote line breaks. However, implementors should be aware that some implementations may use other values." In practice, files written on a Mac or Linux machine often end lines with LF alone.
Why splitting on commas fails
It's tempting to read a CSV by splitting on line breaks and then splitting each line on commas. Wikipedia's CSV article says this example "illustrates that a CSV cannot be parsed by naively splitting the data by line terminators into lines, and then each line by commas." A quoted field can hold both characters. You need a parser that tracks whether it's inside quotes.
If your goal is only to display a small CSV, you can also convert a CSV into a Markdown table, but the quoted fields still need to be parsed first.
Why CSV files differ between programs
RFC 4180 is not a strict standard. Its status memo says it "does not specify an Internet standard of any kind", and it describes the format "that seems to be followed by most implementations." It also says that no formal specification exists, which allows a wide variety of interpretations.
Python's documentation puts the consequence plainly: "The lack of a well-defined standard means that subtle differences often exist in the data produced and consumed by different applications." Wikipedia adds that some programs may not support line breaks inside a field even when properly quoted, and that double quotes may not be parsed correctly.
The text/csv type also has two optional parameters: charset and header, where header is present or absent. The RFC notes that common usage is US-ASCII. If accented characters turn into garbage when you open a file, you are probably looking at encoding problems in a text file rather than a quoting problem.
Reading and writing quoted CSV in Python
Python's csv module follows the doubling rule by default: its doublequote setting defaults to True. It also offers four quoting modes:
QUOTE_MINIMALquotes only fields with special characters such as the delimiter, the quote character,\ror\n.QUOTE_ALLquotes every field.QUOTE_NONNUMERICquotes all non-numeric fields.QUOTE_NONEnever quotes. Special characters get anescapecharprefix, and the writer raises an error if you have not set one.
Here's a short example that writes the tricky values from above:
import csv
rows = [["name", "note"], ["Ada", 'She said "hi", then left\nlater']]
with open("notes.csv", "w", newline="") as f:
csv.writer(f).writerows(rows)Python's docs say a file object should be opened with newline=''. Without it, "newlines embedded inside quoted fields will not be interpreted correctly", and on platforms that use \r\n an extra \r is added on write.
A quick checklist for hand-editing CSV
- Quote any field with a comma, double quote or line break.
- Double every double quote inside a quoted field.
- Never put a double quote inside an unquoted field.
- Keep the same number of fields on every line.
- Don't add spaces after commas unless you want them in the data.
- When in doubt, let a parser write the file.
How Carets fits in
A CSV is a plain text file, and quoting mistakes are easiest to find when you can read the raw text without a spreadsheet hiding it. Carets is a fast, native notes and code editor for iPhone, iPad, and Mac, built for Markdown, plain text, and code. It is native to iOS and Mac rather than a web wrapper, and it organizes your files into projects and tags, so a small data file and the notes that explain it can live together. Your files stay plain text, which keeps them portable and future-proof.
Get Carets on the App Store for iPhone, iPad, and Mac.
Frequently Asked Questions
Do I have to quote every field in a CSV file?
No. RFC 4180 says each field may or may not be enclosed in double quotes. Only fields containing line breaks, double quotes or commas should be quoted. Quoting everything is allowed, and Python's QUOTE_ALL does exactly that.
How do I put a double quote inside a CSV field?
Enclose the field in double quotes and double the inner quote. The RFC's example is "aaa","b""bb","ccc", where the middle field reads b"bb. An unquoted field may not contain a double quote at all.
Can a CSV field contain a line break?
Yes, if the field is enclosed in double quotes. Some programs do not support this, so test any tool you rely on before you depend on it.
Is RFC 4180 the official CSV standard?
Not strictly. It is an Informational RFC from October 2005 that documents what most implementations seem to do, and its status memo says it does not specify an Internet standard of any kind.