RFC 4180 (CSV)

RFC 4180 is the document that defines the common CSV format and registers the text/csv MIME type. Published in October 2005, it is Informational — it wrote down what spreadsheets and databases were already doing rather than imposing a standard — so it is the reference most parsers aim for, not a guarantee every file follows.

The rules, in full

  1. Each record is on its own line, ended by a line break — CRLF (\r\n).
  2. The last record may or may not have a trailing line break.
  3. The first line may be a header with the same number of fields. Whether it is one is signalled out of band, through the MIME type's header=present|absent parameter.
  4. Every record should have the same number of fields. Spaces are part of a field and must not be trimmed. There is no comma after the last field.
  5. Any field may be enclosed in double quotes.
  6. A field containing a comma, a double quote or a line break must be enclosed in double quotes.
  7. A double quote inside a quoted field is written twice: "".
id,name,note
1,Ada,plain
2,"Lovelace, Ada","said ""hi"""
3,Grace,"two
lines"

That file has three records, not four — the line break in record 3 is inside quotes.

What it doesn't cover

Question RFC 4180 says Real files do
Delimiter Comma, always ; in locales where , is the decimal separator
Encoding The charset MIME parameter UTF-8, UTF-8 with a BOM, Windows-1252
Line ending CRLF LF from most Unix tools; parsers accept both
Escape character None — quotes are doubled Some tools emit \", which other parsers misread
Types (numbers, dates, nulls) Nothing — every field is text Excel and Sheets guess, and drop leading zeros
Comments, sep= line Nothing Excel honours a first line of sep=;

Those gaps are where CSV breaks between programs: CSV opens wrong in Excel walks through the encoding, separator and type-coercion cases, and CSV injection covers the field that starts with =.

Reading it correctly

Splitting on \n and then on , fails on rule 6. A correct reader is a small state machine — inside quotes or not — and every standard library has one: Python's csv, Go's encoding/csv, PapaParse or d3.csvParse in JavaScript. Writing is simpler: quote a field when it contains a comma, quote or line break, and double any quotes inside it.

Comma's free converters follow these rules in both directions — CSV to JSON, JSON to CSV, HTML table to CSV and CSV to HTML table.

Try it

Comma is free — unlimited reports, unlimited commenters, unlimited revision history.

Turn a CSV into a shareable table →

Related