When CSV Needs Quotes Around a Value
A comma inside a value is the most common cause of a broken CSV import. Here are the rules, and the tools that fix it in seconds.
CSV looks like the simplest format in computing and it is the one that breaks most often. The reason is almost always the same: a value that contains the separator, and a file that did not quote it.
The rule in one sentence
A field must be wrapped in double quotes if it contains a comma, a double quote, a line break, or leading or trailing whitespace that matters. Inside a quoted field, a literal double quote is written twice. Everything else can stay unquoted.
Why your import gained a column
Take a row like this:
1,Smith, John,Kathmandu, Nepal,40
That looks like six values to a human and eight to a parser. The address split into two fields, the name split into two, and every subsequent column shifted right. Nothing errored; the data is simply wrong, and it is usually discovered weeks later. The correct row quotes the fields that contain commas:
1,"Smith, John","Kathmandu, Nepal",40
Fixing a file without opening Excel
If you have a single line and need to see the real items, paste it into comma-separated list to lines. It is quote-aware, so "Smith, John" stays one item and the split tells you whether the file was correctly quoted in the first place: a value that splits in the middle is evidence that the quotes are missing upstream.
If you have a column and need to build a quoted, comma-separated line, the reverse direction uses add quotes around each line followed by convert newlines to commas.
Four details that catch people out
- Semicolon files exist. Many European exports use a semicolon as the separator because the decimal comma would otherwise collide. If a comma-split file produces one long value, try the semicolon option.
- Excel is not the standard. Excel writes what it likes and reads more leniently than a strict parser. A file that opens cleanly in Excel can still fail in a database loader.
- A leading apostrophe is not a quote. Excel uses a leading apostrophe to force text formatting. It is not part of the CSV quoting rules and will appear as a literal character in the file.
- UTF-8 with a byte order mark can put an invisible character at the start of the first header, which is why the first column name sometimes refuses to match. Save as UTF-8 without BOM for loaders.
Tools used in this guide
- Convert a comma-separated list to lines
Split a comma-separated list into one item per line. Quoted values containing commas stay whole instead of splitting. - Add quotes around each line
Wrap every line in quotes in one pass. Characters inside the line are escaped, so the output is ready for a SQL IN list, a CSV field or a code array. - Convert newlines to commas
Turn a column of lines into one comma-separated line, ready to paste into a query, a form field or a spreadsheet cell. - Remove empty lines
Strip blank lines and lines that contain only spaces or tabs, without touching real content.