Comma-separated values

From Free net encyclopedia

The comma-separated values (CSV) file format is a tabular data format that has fields separated by the comma character and quoted by the double quote character.

In some countries (e.g., Germany) the separator frequently used is a semicolon, allowing the use of the comma in a field without having to enclose the field in quotes. The comma is also used in several countries as a decimal separator (whereas the period is used as the thousands separator, as in "23.000,56"; compare with "23,000.56").

The CSV file format does not require a specific character encoding, byte order, or line terminator format.

Fields which contain an embedded separator character (comma), line-break character, or double-quote character, must be enclosed in double-quotes. Many software implementations do not require that fields are quoted unless they contain one of these characters.

If a field's value contains a double quote character it is escaped with a pair of double quote characters.

A similar format, tab-separated values or .tsv file, is a delimited data format that has fields separated by the tab character. Application support and usage is similar to a comma-separated values file.

Contents

Formal specifications

While no formal specification for CSV exists, there are several informal documents describing the format (1, 2, 3 and 4). The closest thing to a formal specification is RFC 4180.

MIME type

The formal MIME type for CSV is "text/csv" as specified by RFC 4180 and registered by IANA. Additional types that are sometimes used include "application/csv" and "text/x-csv".

Example

1997FordE350ac, abs, moon3000.00
1999ChevyVenture "Extended Edition" 4900.00
1996JeepGrand CherokeeMUST SELL!
air, moon roof, loaded
4799.00

The above table of data may be represented in CSV format as follows:

1997,Ford,E350,"ac, abs, moon",3000.00
1999,Chevy,"Venture ""Extended Edition""",,4900.00
1996,Jeep,Grand Cherokee,"MUST SELL!
air, moon roof, loaded",4799.00

This CSV example illustrates that:

  • fields that contain commas, double-quotes, or line-breaks must be quoted,
  • a quote within a field must be escaped with an additional quote immediately preceding the literal quote,
  • space before and after delimiter commas may be trimmed, and
  • a line break within an element must be preserved.

Application support

The CSV file format is a very simple data file format that is supported by almost all spreadsheet software such as Excel (although some local versions use semicolons instead of commas), Calc, and Gnumeric. Any programming language that has input/output and string processing functionality will be able to read and write CSV files.

CSV is as similar in ubiquity for tabular data as ASCII files are for text data.

Programming language tools

Programming language support for CSV files
Language Tool Notes
BASIC none required supported internally
C/[[C++]] CSV module No comments in code or documentation
Haskell    
Java Several free CSV tools exist:

CSVReader/Writer CSVFile [1] [2] [3] and commercial tools: Ricebridge Java CSV Component. There are also JDBC drivers available: [4] [5] [6] [7] and an ODBC driver: [8]

 
LISP    
Mathematica    
MATLAB csvread, dlmread. In the standard library.
.Net Fast CSV Reader by Sébastien Lorion. Open Source class (MIT licence).

CSV Reader

ExcelLite by GemBox Software for CSV <==> XLS conversion.

 
OCaml OCaml CSV  
Perl Text::CSV_XS, Text::CSV_PP, or using a Perl DBI interface:

DBD::CSV, DBD::AnyData, csvdiff - compare two csv-files

from CPAN
PHP fgetcsv() function In the standard library.
Python Python CSV module In the standard library.
R read.csv In the standard library.
Ruby Ruby CSV module In the standard library.

Utilities

The csvprint utility will reformat CSV input based on a format string. This can be useful for reordering fields or generating source code or tables as illustrated in the following example:

 $ csvprint data.csv "\t{ %0, %1, %2, \"%3\" },\n"
         { 0xC0000008, 0x00060001, NT_STATUS_INVALID_HANDLE, "The handle is invalid." },

External links

de:CSV-Datei fr:Comma-separated values it:CSV ja:カンマ区切りテキスト pl:CSV ru:CSV sv:Csv