.CSV
Comma-separated values
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.
Software
- Microsoft Excel (Windows, macOS, web)
- LibreOffice Calc (Windows, macOS, Linux)
- Google Sheets (web)
- Numbers (macOS, iOS)
- Visual Studio Code (Windows, macOS, Linux)
- Python, whose
csvmodule reads and writes it
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.