Fix Comma and Newline Errors in CSV Fields
Commas, quotes, and line breaks inside CSV fields shift columns and split rows. Learn how quoting works, find the bad row, and fix it step by step.
Few things are more frustrating than a CSV that opens as an unreadable mess: columns shifted three places to the right, 1,000 records ballooning into 1,400 rows, or one giant cell swallowing half your spreadsheet.
In my experience, 99% of the time, the culprit is the same: an unescaped comma, a stray double quote, or an unquoted newline inside a field like a product description or street address.
Here is a practical guide to how CSV quoting really works, the seven ways fields break in real life, how to track down the exact corrupt row, and how to fix it without losing data.
How CSV quoting is supposed to work
CSV has no single universal standard, but nearly every tool follows the same basic rules, described in RFC 4180:
- A field that contains a comma, a double quote, or a line break must be wrapped in double quotes.
- A double quote inside a quoted field is written as two double quotes.
- Every record has the same number of fields as the header.
Here is a valid file:
id,name,notes
1,Alice,"Likes tea, coffee"
2,Bob,"Said ""hello"" twice"
3,Carol,"First line
Second line"
This file has three records, not four. The last record spans two lines because its line break sits inside quotes. A parser that follows the rules reads a table of 3 rows and 3 columns, and the comma, the doubled quotes, and the line break all stay part of the data.
Now the same first record without quotes:
id,name,notes
1,Alice,Likes tea, coffee
The header has three fields, but record 1 now has four. “coffee” lands in a column that doesn’t exist.
Seven ways quoted fields go wrong
1. An unquoted comma inside a field
This is the classic. An address like 12 Main St, Apt 4 or a product name like Cable, 2 m splits into two columns. Whatever program wrote the file did not quote the field. If you generated the file with code, check for string joining such as ",".join(row) and switch to a real CSV writer.
2. A missing closing quote
If a field opens with a quote and never closes it, the parser keeps reading. Depending on the tool, you get an error, or everything from that point on lands in one giant cell, and your row count drops far below what you expected. The broken cell is usually right before the giant one.
3. A stray quote inside an unquoted field
A value like 5" pipe contains a quote but isn’t wrapped in quotes. Strict parsers complain, and lenient ones guess. The correct form is "5"" pipe": wrapped in quotes, with the inner quote doubled.
4. Line breaks inside fields
Text typed with Alt+Enter in Excel, or pasted from a text box or a web form, contains line breaks. They are valid inside quoted fields, but any tool that reads the file line by line, such as a quick script or a line counter, sees extra rows. Line counts like wc -l count lines, not records, so they can disagree with your record count.
5. The wrong delimiter
The file is not comma-separated at all. Many European regional settings use a comma as the decimal mark, so Excel writes “CSV” files with semicolons instead. Tabs and pipes are also common. If your whole file lands in one column, this is the first thing to check.
6. Curly (smart) quotes
Quotes pasted from word processors look like “ ” or ’, but a CSV parser only recognizes the straight double quote (“). A field wrapped in curly quotes is not quoted at all, so any comma inside it splits the field.
7. Encoding and byte-order marks
If accented characters look like é or the first header shows an invisible extra character, the file’s encoding doesn’t match what your tool expects. A byte-order mark (BOM) at the start of a UTF-8 file can attach itself to the first column name and break lookups by header.
How to find the broken row
- Open the raw file in a plain text editor, not in Excel. Excel hides the problem by reinterpreting the data. Turn on “show whitespace” or “show line endings” if the editor has it.
- Compare field counts. Count the fields in the header, then look for the first record with a different count. A short script is the most reliable way to do this, because it understands quotes:
import csv
with open("data.csv", newline="", encoding="utf-8-sig") as f:
reader = csv.reader(f)
header = next(reader)
for record in reader:
if len(record) != len(header):
print("near line", reader.line_num, "->", len(record), "fields:", record[:3])
The newline="" argument matters: Python’s documentation requires it so that line breaks inside quoted fields are read correctly. The utf-8-sig encoding strips a BOM if there is one.
- Look at what comes right before the first bad record. A missing closing quote almost always sits just before the point where the structure goes wrong.
- Check it in a viewer. Open the file in the CSV viewer. If a quoted field with a comma stays in a single cell, the columns line up, and the row count matches what you expect, the file is well-formed. A cell holding a huge block of text points you to the missing quote.
How to fix each problem
Quote fields that contain commas, quotes, or line breaks. Fix this where the file is created. Spreadsheet programs and CSV libraries (Python’s csv module, pandas’ to_csv, JavaScript libraries such as Papa Parse) add the quotes for you. Hand-built strings are where the errors come from.
Double inner quotes. Replace each " inside a field with "" and keep the field wrapped in quotes. A proper CSV writer does this automatically.
Remove or replace line breaks if the tool that receives the file can’t handle them. In pandas, you can flatten them like this:
import pandas as pd
df = pd.read_csv("data.csv", dtype=str, keep_default_na=False)
df = df.replace(r"[\r\n]+", " ", regex=True)
df.to_csv("data-clean.csv", index=False, encoding="utf-8")
Tell your tool the delimiter. In Excel, don’t double-click the file. Use Data > From Text/CSV, pick the delimiter, and check the preview before loading (menu names vary by version). In pandas, use sep=";", or sep=None, engine="python" to let it detect the delimiter.
Replace curly quotes only where they act as delimiters. If they are part of the text, such as a typographic apostrophe in a product name, leave them alone.
Re-save as UTF-8. In Excel, choose “CSV UTF-8 (Comma delimited)” in Save As, which keeps accents intact.
When you only need to find out how many rows are affected, you can let pandas skip and report bad lines:
df = pd.read_csv("data.csv", on_bad_lines="warn")
Use this to diagnose, not to produce your final file. Skipping bad lines silently drops data.
Quick reference
| What you see | Likely cause | What to do |
|---|---|---|
| Some rows have more columns than the header | An unquoted comma in a field | Quote the field, or fix the export |
| Everything after row N is in one cell | A missing closing quote | Find the quote that never closes |
| More rows than records | Line breaks inside fields | Use a real CSV parser, or flatten the line breaks |
| All data in one column | Wrong delimiter | Choose the delimiter when you open the file |
| Strange characters like é | Encoding mismatch | Re-save the file as UTF-8 |
| Odd character in the first header | A byte-order mark | Read with utf-8-sig, or re-save without it |
Test the fixed file
Before you import the file anywhere, load it once more in the CSV viewer and check three things: the number of columns is the same on every row, the record count matches your source, and the long text fields still sit in one cell each. If you do not have a file of your own to practice on, download one of our free sample CSV files and run the checks above on it. If you are combining several exports, fix the quoting in each file first, then merge the CSV files. To confirm the data survives a real conversion, run it through the CSV to JSON converter and look at a few records.
Working with product data? The same quoting problems show up in e-commerce feeds, where descriptions are full of commas, quotes, and line breaks. See our guide on cleaning product CSVs for Shopify and WooCommerce. If the files contain private data, read why you shouldn’t upload sensitive CSVs to cloud converters before choosing a tool.
Frequently asked questions
How do I include a comma in a CSV field?
Wrap the whole field in double quotes, for example "Likes tea, coffee". Parsers treat a comma inside the quotes as data instead of a column separator. Most spreadsheet programs and CSV libraries add these quotes automatically when they write the file.
How do I put a double quote inside a quoted CSV field?
Double it. The text Said "hello" is written as "Said ""hello""", with the whole field wrapped in quotes. A single unescaped quote inside the field is read as the end of the field, and it usually breaks the row and shifts the columns after it.
Can a CSV field contain a line break?
Yes, as long as the field is wrapped in double quotes. A parser that follows the CSV rules reads it as part of one record. Tools that split the file line by line will see extra rows, so test the file in a viewer or a real CSV parser.
Why does my CSV open in a single column in Excel?
Excel splits CSV files using your system’s list separator, which is often a semicolon in regions where the comma is the decimal mark. Open the file with Data > From Text/CSV and choose the delimiter yourself. Menu names vary a little between Excel versions.
What does “Error tokenizing data” mean in pandas?
It means a line has a different number of fields than the parser expected, usually because of an unquoted comma or a missing quote. Find the line number in the error, look at the raw text around it, and fix the quoting there.