🧹 Data Cleaning · 6 min read
How to Check a CSV File for Errors Before You Import It
Seven checks, and the first one catches most of them: every row must have the same number of fields as the header. After that, confirm the file is not truncated, that the encoding matches what the importer expects, that the delimiter is the one it assumes, that column names are unique, that no invisible characters sit in the first column, and that the file is inside the row and column limits of whatever will read it. Ten minutes of checking saves an afternoon of debugging a failed load.
Every defect below has a symptom you have probably seen: an import that reports a duplicate key, a column of values that shifted one cell to the right, a date column that arrived as text. None of those are caused by the importing system, and all of them are visible in the file before the import runs.
1. Is the file complete?
Check the size and then the last line. A download that was interrupted, or an export job that died halfway, produces a file whose final record stops mid-field. Open it in a text editor and look at the end: a complete record ends with a value and a line break, an incomplete one ends inside a quoted field.
Byte order matters at the other end. A UTF-8 file saved by Excel usually starts with the three bytes EF BB BF, the byte order mark. Most parsers strip it, some do not, and the ones that do not give you a first column called \ufeffid instead of id. If your first column name looks right but every lookup on it fails, that is the reason. Reading with encoding="utf-8-sig" removes it.
2. Does the encoding match?
A file can be UTF-8, UTF-16, or a single-byte code page such as Windows-1252, and the extension tells you nothing. The tell is in the text: accented characters that arrive as é or as question marks mean the file was written in one encoding and read in another.
# show the first bytes; EF BB BF means UTF-8 with BOM, FF FE means UTF-16 LE
xxd -l 4 data.csv
# try a strict decode: raises on the first byte that is not valid UTF-8
python3 -c "open('data.csv', encoding='utf-8').read(); print('valid UTF-8')"If the strict decode fails, the file is not UTF-8 and no amount of retrying the import will make it so. Convert it once with the right source encoding, and note which one it was.
3. Is the delimiter the one the importer assumes?
Comma is the default in the English-speaking world and semicolon is the default in much of Europe, because the comma is the decimal separator there. A file exported from a semicolon-locale system and imported with a comma setting arrives as one column, which is the symptom described in a CSV that opens in one column.
Count the candidate separators in the first line. The one that appears consistently, and the same number of times in the second line, is the delimiter. Tabs produce the same problem with a different character, and a file with a tab delimiter is a TSV whether or not it is named CSV.
4. Does every row have the header's field count?
This is the check that finds the real damage. A value containing an unquoted comma adds a field, and everything after it slides one column to the right. The row still looks like data, which is why it survives a visual check and destroys a totals column.
import csv, collections
with open("data.csv", newline="", encoding="utf-8-sig") as f:
reader = csv.reader(f)
header = next(reader)
counts = collections.Counter(len(r) for r in reader)
print("header fields:", len(header))
print("row length distribution:", dict(counts))A single entry in that distribution means the file is structurally sound. A spread of two or three lengths means some rows are long or short, and those are the rows to open. RFC 4180 allows a field to contain the delimiter, a quote or a line break, but only when the field is wrapped in double quotes and an internal quote is doubled, so a parser will not report an error for a correctly quoted comma. It reports the ones that were never quoted.
5. Are the column names unique and present?
Two columns called amount, an empty header cell, or a trailing empty column from a spreadsheet export are all common, and all of them break a loader that maps columns by name. Duplicate names also break the one-line-per-record assumption in tools that convert CSV to JSON, because a key can only appear once in an object.
Strip whitespace from names, replace spaces with underscores if the target system dislikes them, and check for the empty string. It is cheaper to rename a column in the file than to discover at load time that two of them collided.
6. Anything invisible in the data?
Leading and trailing spaces survive an export and quietly break joins: "ACME " does not match "ACME". Non-breaking spaces, pasted in from a web page, look identical to a normal space on screen. Zero-width characters do too.
A quick pass that strips both ends of every field and collapses internal runs of whitespace fixes the visible cases. For the invisible ones, search the file for the byte sequences: C2 A0 is a non-breaking space, E2 80 8B is a zero-width space. If either appears, decide deliberately whether it should be there before removing it.
7. Is the file inside the limits of the thing reading it?
A CSV has no row limit of its own, but almost everything that opens one does. Excel stops at 1,048,576 rows, 16,384 columns, and 32,767 characters in a single cell, and it does not warn you that it stopped. Databases, CRMs and import wizards have their own, usually lower. Two questions worth answering before an upload: how many rows does this file actually have, and what is the destination's limit.
Our CSV analyzer reports row count, column count and the longest cell, which answers the first question without opening a spreadsheet. For a file that exceeds the limit, cutting it into pieces is the usual answer, and the splitter does that by row count or by size.
When the check finds something
Re-export from the source system if you can. A hand-edited file loses the audit trail, and the defect usually returns with the next export because the cause was upstream. Fix in place only when the source is gone and you can list exactly which rows you changed.
If the problems are scattered rather than structural, duplicated rows, blank rows, stray characters in half the records, the fix is closer to cleaning than to validation, and cleaning dirty CSV data is the right next page. The two jobs are different: validation tells you the file is wrong, cleaning makes it right.
Frequently asked
What is the fastest way to check a CSV for errors?
Read the header, then check that every row has the same number of fields as the header. That comparison catches the most common defect, an unquoted comma inside a value, because it shifts the rest of the row and produces one extra field. A parser that counts fields per row reports it in a second.
Why does a CSV import fail when the file opens fine in Excel?
Excel is forgiving in ways a database is not. It opens a file with mixed encoding, a missing header, duplicated column names or uneven field counts, because it guesses and displays the result. An importer expects a fixed schema, so the same file fails at the first row whose field count does not match. The file was always wrong; Excel hid it.
How do I find the bad row in a large CSV?
Let the parser tell you. Importers usually report the failing line number, and a script that counts fields per row prints the line numbers where the count differs from the header. Do not look for it by eye in a spreadsheet: a shifted row looks normal, because the values are still values, just under the wrong headings.
Should I fix the file or re-export it?
Re-export from the source system whenever you can. A hand-edited CSV loses the audit trail and often fixes the symptom rather than the cause, so the next export reproduces the same defect. Fix in place only when the source is gone and you can state exactly which rows you changed.
Sources for this article: RFC 4180 for the CSV grammar, quoting and line-break rules; Python documentation for csv.reader, newline="" handling and the utf-8-sigcodec; Microsoft Support's Excel specifications and limits for the 1,048,576 row, 16,384 column and 32,767 character figures; and the Unicode standard for the code points behind the byte sequences named above. Figures checked on the day this page was published.
Related reading
Data Cleaning — other guides that pair well with this one.
- Alteryx "Too Many Fields in Record" on a CSV: Causes and Fixes
- Excel Says the File Format and Extension Don't Match. What Now?
- CSV Import Skipped Rows? Where They Went and How to Get Them Back
- CSV to TXT: What Converting to Plain Text Actually Means
Browse all guides in the NoCodeCSV blog.