CSV Files: Common Pitfalls and How to Avoid Them

6 min read · Published 1 November 2026 · Updated 2 November 2026

CSV (comma-separated values) is the simplest way to move table data between programs, and the format most spreadsheets can open. Its simplicity is also the source of many bugs, because "CSV" is a loose convention rather than one strict format. This guide covers the problems that appear most often, and how to avoid them.

The basic rules

The informal standard, RFC 4180, says: each record is on its own line, fields are separated by commas, and the first line may be a header. A field that contains a comma, a line break or a double quote must be wrapped in double quotes, and a double quote inside such a field is written twice.

Example

id,name,note
1,"Nguyễn, Văn A","He said ""hello"""
2,Lan,plain text

Do not split on commas

The most common mistake in code is line.split(","). It breaks on quoted fields that contain commas or line breaks. Use a proper CSV parser (every major language has one), which handles quoting, escaped quotes and multi-line fields.

The delimiter is not always a comma

In many countries the comma is the decimal separator (for example 3,14), so spreadsheet programs there export "CSV" files separated by semicolons. Tab-separated files (TSV) are also common. If a file opens as a single column, the delimiter probably differs from what your program expects. Detect it, or ask the sender to state it.

Character encoding and accented text

A CSV file carries no information about its encoding. UTF-8 is the right choice today, but some spreadsheet programs assume an older encoding unless the file starts with a byte order mark (BOM). The result is garbled text for names with accents, for example Vietnamese letters appearing as strange symbols. Save and read as UTF-8, add a BOM if the target spreadsheet requires it, and test with real accented text, not just plain English.

Spreadsheets "helpfully" change your data

  • Leading zeros disappear: the phone number 0912345678 becomes 912345678, and the postal code 01234 becomes 1234.
  • Long numbers become scientific notation (1.23457E+15), which destroys identifiers such as barcodes and account numbers.
  • Text that looks like a date is converted. Entries such as "1-2" or gene names like SEPT1 turn into dates.
  • To avoid this, import the file with the column format set to "text", or store such fields as text and quote them explicitly in your own tools.

CSV injection (formula injection)

When a spreadsheet opens a cell that begins with =, +, - or @, it may treat the text as a formula. An attacker who can get such text into data that you export, such as a name typed into a form, can make the spreadsheet run formulas that leak data or open links on whoever opens the file. When you generate CSV from user input, prefix risky values with a single quote or a space, or otherwise neutralise them, and treat downloaded files with care.

Inconsistent rows and hidden problems

  • Rows with a different number of fields than the header, often caused by unquoted commas.
  • Trailing empty columns and blank lines at the end of the file.
  • Mixed line endings (Windows CRLF and Unix LF) in the same file.
  • Different spelling of empty values, such as "", NULL, N/A or a space.

Comparing two CSV exports

To find what changed between two exports, make the comparison fair first. Use the same encoding, delimiter and line endings. Sort the rows by a stable key (such as an ID) in both files, because exports often return rows in a different order. Then compare them with a diff tool and read the changed rows. For large files, compare only the columns that matter.

When CSV is the wrong choice

CSV has no types, no nested data and no metadata. If your data has hierarchy, long text with many line breaks or fields where exact types matter, JSON or a database may be a better format. CSV remains an excellent choice for flat tables that people will open in a spreadsheet.

Frequently asked questions

Why does Excel show strange characters for my Vietnamese text?
The program is reading the file in a different encoding. Save the file as UTF-8, with a BOM if necessary, or import it using the data import option and choose UTF-8.
What is the difference between CSV and TSV?
TSV uses tab characters as separators instead of commas. Tabs appear less often inside text, which reduces quoting problems, but the file still has no standard handling of embedded tabs and line breaks.
Can a CSV file have several sheets?
No. A CSV file holds one table. Formats such as XLSX can hold several sheets.
How do I keep a leading zero?
Treat the column as text when importing into a spreadsheet, and keep the value as a string in your own code. A leading zero is part of the text, not of a number.

Sources and further reading

Try it yourself: File Compare runs in your browser, free and with no sign-up.

More guides