CSV looks like the simplest format in the world — values separated by commas. Then a value contains a comma, or a quote, or a line break, and a naive split() quietly corrupts your data. Here's what the format actually requires.
CSV feels like the format you don't need to think about. Values separated by commas, rows separated by line breaks — you could parse it with a one-liner. And you can, right up until a customer's name is "Smith, Jr.", or an address field contains a line break, or someone pastes a quote character into a comment. At that point a naive parser doesn't throw an error; it silently shifts every column after the break and hands you plausible-looking, wrong data. That silence is what makes CSV bugs so costly. Let's look at exactly where they come from, using one small file we generated and parsed with Python's csv module.
Why "split on commas" is a trap
The comma is doing double duty. It separates fields, but it's also an ordinary character that appears inside real data all the time — in names, in prices written the European way, in free-text notes. The moment a field's own content contains the delimiter, splitting on that delimiter cuts the field in half and every subsequent column is off by one. Nothing in the row looks malformed afterwards; you just have a value in the wrong place and a total that no longer adds up three reports downstream. This is the core problem CSV has to solve, and it solves it with quoting.
The actual specification
There is a written description — RFC 4180 — and it's short enough to read in one sitting. It's worth knowing its status: the RFC says of itself that it "does not specify an Internet standard of any kind". It documents the format "followed by most implementations", which is why real files still vary. The rule that matters most is how fields containing awkward characters get protected:
"Fields containing line breaks (CRLF), double quotes, and commas should be enclosed in double-quotes. [...] If double-quotes are used to enclose fields, then a double-quote appearing inside a field must be escaped by preceding it with another double quote."
— RFC 4180, "Common Format and MIME Type for Comma-Separated Values (CSV) Files"
Read that twice, because it resolves all three edge cases at once. A comma inside a field is fine — as long as the field is wrapped in double quotes. A line break inside a field is fine — same rule. And a double quote inside a field is represented by doubling it: "" means one literal quote.
Our test file
We wrote three awkward records with Python's csv.writer and got this for the first one:
1,"Smith, Jr.","She said ""hi""","1,50"
Each awkward field is quoted, the inner quotes are doubled, and the European-style price 1,50 is protected too. The second record holds the name Kováč and a two-line note; the third has a note of 5" screen and an empty price. Reading the file back with csv.reader returned exactly the values we wrote. Splitting the same lines on commas did not: the first record came out as six fields instead of four.
Edge case one: commas inside a field
The classic. The value Smith, Jr. must be written as "Smith, Jr.". A compliant parser sees the opening quote, reads until the matching closing quote, and treats every comma in between as data. A naive line.split(",") sees the comma as a delimiter and produces a phantom extra column — in our first record, two of them, because the price had a comma as well. The fix isn't to ban commas in your data — it's to use a parser that honours quoting, and to make sure whatever writes your CSV quotes fields correctly in the first place.
Edge case two: quotes inside a field
This is the one people get wrong even after they've handled commas. Once you're using double quotes to wrap fields, a literal quote inside the data is ambiguous — is it closing the field or is it content? RFC 4180's answer is doubling: two quotes in a row inside a quoted field mean one literal quote. Our writer turned 5" screen into "5"" screen".
Here's a subtler trap: many readers are lenient. Python's reader accepted an unquoted 5" screen as a plain field, because the quote wasn't at the start. A stricter parser might reject the same line. So a file can work in one tool and fail in another, which is why it's the writer's job to quote correctly rather than relying on the reader to forgive.
Edge case three: newlines inside a field
The most surprising one, because it violates the mental model that "one line equals one row." A quoted field is explicitly allowed to contain line breaks. So a multi-line address stored as a single field spans several physical lines, and the row isn't finished until the closing quote appears. You cannot parse CSV by reading it line by line and splitting each line — a single logical record may occupy five lines of text. A parser has to track whether it's currently inside a quoted field.
In Python, that has a practical consequence. The csv module documentation says that if the file is a file object, "it should be opened with newline=''". Skip that and newline translation can interfere with line breaks inside quoted fields.
The CRLF and encoding footnotes
RFC 4180 describes CRLF (\r\n) as the line break between records, and that's what Python's default writer produced. Real-world files often arrive with plain \n, so a robust reader accepts both.
CSV carries no encoding declaration of its own — a file is just bytes. Our Kováč is the UTF-8 bytes 4B 6F 76 C3 A1 C4 8D. Decoded as Windows-1252 instead, it came out as KovÃ¡Ä followed by an undecodable byte. When accented names turn to mojibake, the format isn't the culprit; a mismatched encoding is. Some spreadsheet software looks for a byte-order mark to recognise UTF-8; Python's utf-8-sig codec writes one (EF BB BF) at the start of the file. Other consumers treat those bytes as part of the first column name, so whether to include it depends on who reads the file.
Not every "CSV" uses commas
In locales where the comma is the decimal separator, spreadsheets often export with semicolons instead. Written with delimiter=';', our first record became 1;Smith, Jr.;"She said ""hi""";1,50 — the name and the price no longer need quotes, because a comma isn't special any more. Python's default comma reader split that line into three meaningless fields: 1;Smith, Jr.;"She said ""hi""";1 and 50. Python's csv.Sniffer can guess the dialect from a sample, but a guess is not a contract; agree on the delimiter with whoever produces the file.
Common mistakes
- Splitting on commas or reading line by line.
- Writing CSV by string concatenation instead of a CSV writer.
- Assuming the encoding instead of agreeing on it.
- Assuming every file uses commas and a header row (RFC 4180 calls the header optional).
Round-trip it, don't hand-roll it
The reliable way to know your CSV survives these edge cases is to round-trip it: write it with a real CSV writer, read it back with a real reader, and compare. Converting to JSON is a good way to look at the result, because every field becomes an explicit string — a field that should contain a comma or line break either comes through whole or it doesn't. Our three records came back as clean JSON objects with "Smith, Jr." and "line one\nline two" intact. When that JSON looks tangled, a JSON formatter makes the structure readable so you can check the data before you convert back. CSV isn't hard — it's just unforgiving of the assumption that a comma is always a separator.