An address or a price with a comma can shift every column out of place. Here is why it happens and how to fix it for good.
You open a customer export and the columns are wrong from row 37 onward. The city is sitting in the address column, the postcode has moved under "phone", and a new unnamed column has appeared at the end. Nine times out of ten the cause is the same: a value somewhere contains a comma, and whatever wrote or read the file treated that comma as a column break.
This guide explains why that happens, the quoting rules that prevent it, how to repair a file that is already broken, and how to stop it happening at export time. The examples below were run through our free CSV to JSON converter, so the outputs are real, not idealised.
CSV has no column markers other than the delimiter itself. Each line is a record, and each comma starts a new field. That works until the data contains the delimiter: an address like 12 Park Rd, Apt 4, a price written 1,299, a company called Smith, Jones & Co, or a free-text comment typed by a person. Here is a file where nobody protected the address:
name,address,city
Asha,12 Park Rd, Apt 4,Trichy
The header promises three columns, but the data line has four values. A strict reader has no way to know which comma was meant to be data. Our converter reads it exactly as written and returns:
[{"name": "Asha", "address": "12 Park Rd", "city": "Apt 4", "column4": "Trichy"}]
Nothing was lost, but everything after the address is one column to the right. The converter names the extra value column4 rather than dropping it, which is usually the first clue that something is off.
Misalignment rarely affects every row. It starts at the first record whose value contains a comma and only affects that record, which is why a file can look fine at the top and fall apart halfway down. Common culprits:
values.join(",") or string-concatenates fields never adds quotes.1,299 becomes two fields, 1 and 299.Widget;1,50. Open that with a comma reader and every row splits in the wrong place.The closest thing CSV has to a standard, RFC 4180, solves this with double quotes. Any field may be wrapped in "...", and a field that contains the delimiter, a double quote or a line break must be. Inside the quotes, commas are just text:
name,address,city
Asha,"12 Park Rd, Apt 4",Trichy
[{"name": "Asha", "address": "12 Park Rd, Apt 4", "city": "Trichy"}]
Line breaks work the same way. This is one record with two fields, not two records:
id,notes
1,"line one
line two"
The converter returns {"id": 1, "notes": "line one\nline two"}. If you count lines with a simple tool such as wc -l you will get a higher number than the real record count, which is normal for files like this.
Two details trip people up. First, the quote has to be the first character of the field; a quote in the middle, as in 27" screen, is treated as an ordinary character (our converter keeps it as 27" screen), but stricter readers may reject it. Second, spaces outside the quotes matter to some programs. Our converter trims spaces around unquoted values and keeps spaces inside quotes, so " padded " stays padded.
If quotes mark the edges of a field, how do you put a quote inside one? You double it. Each "" inside a quoted field means one literal ":
name,comment
Ravi,"He said ""hi"" twice"
That reads as the value He said "hi" twice. Backslash escaping (\") is not part of RFC 4180; Excel, Google Sheets and our converter do not treat a backslash specially, so files from tools that use it need the separate option those tools provide (for example the escapechar setting in Python's csv module).
The one mistake that cannot be guessed around is an unclosed quote. In Asha,"12 Park Rd, Apt 4 the field never ends, so everything after it, possibly the rest of the file, would become one giant value. Rather than return a silently mangled table, our converter stops with the error a quoted field is not closed (missing "), which tells you to look for a stray quote.
The best repair is a re-export from the original source with quoting switched on, because only the source knows where each value really ended. When that is not possible:
column4, column5 ...) to see which records overflowed.^([^,]*,){3,}[^,]*$ with the multiline flag matches lines with more than two commas, which is every overflowing line in a three-column file that has no quoted fields yet.Widget;1,50, it is not broken, it is semicolon-separated. Auto-detect in our converter picks semicolon for it and keeps 1,50 as the string "1,50".columnN keys remain.Keep a copy of the original before editing. Bulk find-and-replace on commas almost always does more damage than it fixes, because it cannot tell a separator from data.
Almost every serious tool quotes correctly if you let it do the writing:
csv module (csv.writer quotes only fields that need it by default) or pandas to_csv, never ",".join(row).COPY ... WITH (FORMAT csv) quotes as needed; MySQL's SELECT ... INTO OUTFILE needs FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'.When the receiving side is fixed and you cannot change it, agree on a delimiter that never appears in the data, such as tab, and say so in the file name or documentation.
JSON does not have this problem, because every string is wrapped in quotes and every key is named, so a comma inside a value can never be mistaken for structure. If the data is going into an API, a NoSQL database or a JavaScript app, converting once and passing JSON along removes a whole class of bugs. For a longer comparison, see CSV vs JSON: which data format should you use?
Going the other way, our JSON to CSV converter quotes values for you. The record {"name": "Asha", "address": "12 Park Rd, Apt 4", "note": "He said \"hi\""} comes out as:
name,address,note
Asha,"12 Park Rd, Apt 4","He said ""hi"""
Both converters run in your browser, so the file you paste is not uploaded to our server.
The file uses a different delimiter from the one your Excel locale expects, for example commas on a system set to semicolons. Use Data, From Text/CSV and choose the delimiter, or convert the file with a tool that lets you pick it.
No. Only fields that contain the delimiter, a double quote or a line break must be quoted. Quoting every field is also valid and some systems prefer it.
Yes, if you are sure it never appears in the data. Tab and pipe are common choices. It moves the problem rather than solving it, so quoting is still the safer default.
Wrap the field in double quotes and double the inner quote: "He said ""hi""" reads as He said "hi".