Home › Guides › Developer › CSV pitfalls and how to avoid them

CSV pitfalls: quoting, encodings and spreadsheets

The CSV problems that corrupt data: quoting, delimiters, UTF-8 and the BOM, line endings, leading zeros and formula injection.

By the tools4free.dev team · Last reviewed 4 December 2026

CSV looks simple, but there is no single strict standard, and most problems come from five places: fields that contain commas, quotes or line breaks; the wrong delimiter for the reader's locale; text encoding and the byte order mark; spreadsheet programs that reinterpret values such as 007; and cells that begin with = and run as formulas. RFC 4180 describes the common format, and following it plus a few defensive habits avoids almost every failure.

What RFC 4180 says

RFC 4180 is an informational document that records the common practice and registers the text/csv type. Its rules are short:

The RFC also says it is not a formal standard, and many files in the wild break it. Real parsers accept bare LF line endings, semicolons, tabs and other variations, so a reader should state or detect which dialect it uses.

Worked example 1: quoting rules

Take five values: Smith, John, He said "hi", a text with a line break inside, plain and an empty string. A writer that quotes only when needed produces this, which we generated in Node:

"Smith, John","He said ""hi""","line1
line2",plain,

The first value is quoted because of the comma. The second is quoted and each inner quote is doubled. The third is quoted because of the line break, which then sits inside the field and makes one record span two physical lines. plain needs nothing, and the empty value is just nothing between two commas. The practical lesson: never split a CSV file on newlines or commas yourself. A naive line.split(',') breaks the first value into two fields and the third record into two rows.

Parsing the other way round works with a proper parser. Running the text name,note followed by the two records "Smith, Jo","said ""hi""" and Ann,"two, a line break, lines" through the parser behind our CSV and JSON converter in Node gave two objects: {name: "Smith, Jo", note: 'said "hi"'} and {name: "Ann", note: "two\nlines"}.

Delimiters and locale

The comma is the standard separator, but spreadsheet programs choose the separator from the system's regional settings. In many European locales the comma is the decimal mark, so the list separator is a semicolon, and Excel both writes and expects semicolon-separated files there. A comma-separated file opened by double-click in such a locale may land in a single column. Options are to import through the data import dialog and pick the delimiter, to use a tab-separated file, or to agree on the delimiter with the recipient. In a machine-to-machine exchange, state it in the documentation and always use a parser that takes the delimiter as a parameter.

Decimal marks cause the matching problem inside the data: 3,14 in a comma-delimited file must be quoted, and a reader in the wrong locale may turn 3.14 into a date or text.

Encoding and the byte order mark

CSV has no way to declare its character encoding, so the reader has to guess. UTF-8 is the right default for new files. A byte order mark (BOM) is the three bytes EF BB BF at the start of the file, which is the encoded form of U+FEFF. We confirmed this in Node: Buffer.from('').toString('hex') is efbbbf.

The BOM cuts both ways:

If the file is for people who will double-click it in Excel, write the BOM. If it feeds code, leave it out and say that the file is UTF-8.

Line endings

RFC 4180 specifies CRLF, Unix tools write LF and old Mac files used CR. Good parsers accept all three, but simple ones do not. A stray CR left at the end of the last field is a common cause of values that look equal but fail a comparison. Line breaks inside quoted fields are data and should be kept as written. Whatever you choose, use it consistently within a file.

Spreadsheets reinterpret your data

CSV has no types. Everything is text until a program decides otherwise, and Excel and similar programs decide eagerly when they open a file:

Value in the fileWhat the spreadsheet may showWhy
0077Read as a number, so leading zeros are dropped.
0123456789012345678A shortened or rounded numberNumbers keep about 15 significant digits.
1E5100000Read as scientific notation.
3-4 or MARCH1A dateDate guessing. Some gene names such as SEPT1 were famously converted to dates.

Worked example 2: leading zeros and long numbers

The identifier 007 is a number to a spreadsheet: in Node, Number('007') is 7, and the zeros are gone. Long identifiers fare worse. Parsed as a 64-bit float, which is also what JavaScript does, 12345678901234567890 becomes 12345678901234567000, and 0123456789012345678 becomes 123456789012345680: the last digits are changed, not just reformatted. Credit card numbers, phone numbers and order references should therefore be treated as text. Quoting does not help, because quotes are removed by the parser before the type guess happens. The defences are to import the column as Text through the import dialog, or to set the cell's format to Text before pasting. In files meant to be opened by double-click, some producers write ="007", which Excel evaluates as the text 007, but that corrupts the value for every other reader. Prefer fixing the import.

Formula injection

If a CSV file is opened in a spreadsheet, a cell whose text starts with =, +, - or @ may be executed as a formula. OWASP also lists a leading tab or carriage return. Attackers exploit this by putting such text in a field that your system later exports, such as a name or a comment, so the export carries a malicious formula to whoever opens it. Depending on the program, the formula can read other cells, send data to another server or run commands after the user accepts a prompt.

OWASP's guidance for an export that will be opened in a spreadsheet is to prefix a dangerous cell with a single quote, which makes the spreadsheet treat it as text, and to review whether any value really needs to start with those characters. Note that this changes the data, so it is a presentation-layer decision that belongs in the export code for spreadsheet users and not in your database. A numeric field such as -5 will also match the rule, so apply it to free text and not to numbers. Validate input as well, but do not rely on validation alone.

Common mistakes

Frequently asked questions

Is CSV an official standard?

RFC 4180 describes it and registers text/csv, but it is informational and real files vary. Treat it as the safest common ground.

Should I quote every field?

You may, and it is always valid. Quoting only when needed gives smaller files. Both parse the same.

How do I keep Excel from changing my values?

Import through the data dialog and set the affected columns to Text. Opening by double-click gives you no control.

What is better for APIs, CSV or JSON?

JSON for nested or typed data. CSV for flat tables that people will open in a spreadsheet.

Try it: convert in either direction with the CSV and JSON converter. It handles quoted fields, embedded line breaks and your choice of delimiter, and runs in your browser.

Sources