Introduction
CSV is a plain text table: one record per line, with the fields of each record separated by commas. It has been used to move data between spreadsheets and databases since the early days of personal computing, and almost every program that handles tables can read and write it.
There was no formal standard for decades. RFC 4180, published in 2005, describes the common form. Programs still differ in the details, such as the character encoding and the separator: spreadsheets in many European locales use a semicolon, because the comma is their decimal mark.
Structure
A CSV file has no header bytes or magic number. Following RFC 4180:
- Each record is one line, ended by a carriage return and line feed (many files use a line feed alone).
- Fields are separated by commas. Every record should have the same number of fields.
- The first line may be a header naming the columns. Nothing in the file says whether it is.
- A field that contains a comma, a double quote or a line break is written inside
double quotes, and a double quote inside it is written twice:
"say ""hi""".
Every value is text. Numbers, dates and empty values are interpreted by the program reading the file. Files written by Excel often start with a UTF-8 byte order mark so that Excel reads them as UTF-8 when it opens them again. The grammar and dialects page gives RFC 4180’s rules and the settings in which real files differ.
Links
Software
- Microsoft Excel – Microsoft’s spreadsheet, which opens and saves CSV (Windows, macOS, web)
- LibreOffice Calc – free spreadsheet that asks for the separator and encoding on import (Windows, macOS, Linux)
- Google Sheets – Google’s online spreadsheet, which imports and downloads CSV (web)
- Numbers – Apple’s spreadsheet (macOS, iOS)
- Visual Studio Code – free code editor, for reading and editing CSV as text (Windows, macOS, Linux)
Developers
- csv – Python’s built-in CSV reader and writer (Python)
- pandas – reads and writes CSV as data frames (Python)
- Apache Commons CSV – CSV reader and writer (Java)
- Papa Parse – CSV parser for browsers and Node.js (JavaScript)
Reference
- RFC 4180 – Common Format and MIME Type for Comma-Separated Values (CSV) Files