2. Definition of the CSV Format
Although there are various specifications and implementations for the CSV format (for example, [4], [5], [6], and [7]), there is no formal specification, which allows a wide variety of interpretations of CSV files. This section documents the format that most implementations appear to follow.
Format Rules
Rule 1: Records and Line Breaks
Each record is located on a separate line, delimited by a line break (CRLF). For example:
aaa,bbb,ccc CRLF
zzz,yyy,xxx CRLF
Rule 2: Final Record
The last record in the file may or may not have an ending line break. For example:
aaa,bbb,ccc CRLF
zzz,yyy,xxx
Rule 3: Optional Header Line
There may be an optional header line appearing as the first line of the file, with the same format as normal record lines. This header contains names corresponding to the fields in the file and should contain the same number of fields as the records in the rest of the file. The presence or absence of the header line should be indicated via the optional "header" parameter of this MIME type. For example:
field_name,field_name,field_name CRLF
aaa,bbb,ccc CRLF
zzz,yyy,xxx CRLF
Rule 4: Fields and Commas
Within the header and each record, there may be one or more fields separated by commas. Each line should contain the same number of fields throughout the file. Spaces are considered part of a field and should not be ignored. The last field in the record must not be followed by a comma. For example:
aaa,bbb,ccc
Rule 5: Use of Double Quotes
Each field may or may not be enclosed in double quotes; however, some programs, such as Microsoft Excel, do not use double quotes at all. If fields are not enclosed with double quotes, then double quotes may not appear inside the fields. For example:
"aaa","bbb","ccc" CRLF
zzz,yyy,xxx
Rule 6: Handling Special Characters
Fields containing line breaks (CRLF), double quotes, and commas should be enclosed in double quotes. For example:
"aaa","b CRLF
bb","ccc" CRLF
zzz,yyy,xxx
Rule 7: Escaping Double Quotes
If double quotes are used to enclose fields, then a double quote appearing inside a field must be escaped by preceding it with another double quote. For example:
"aaa","b""bb","ccc"
ABNF Grammar Definition
The ABNF grammar [2] is as follows:
file = [header CRLF] record *(CRLF record) [CRLF]
header = name *(COMMA name)
record = field *(COMMA field)
name = field
field = (escaped / non-escaped)
escaped = DQUOTE *(TEXTDATA / COMMA / CR / LF / 2DQUOTE) DQUOTE
non-escaped = *TEXTDATA
COMMA = %x2C
CR = %x0D ; as per Section 6.1 of RFC 2234 [2]
DQUOTE = %x22 ; as per Section 6.1 of RFC 2234 [2]
LF = %x0A ; as per Section 6.1 of RFC 2234 [2]
CRLF = CR LF ; as per Section 6.1 of RFC 2234 [2]
TEXTDATA = %x20-21 / %x23-2B / %x2D-7E
Implementation Notes
- Character set: Although common usage of CSV is US-ASCII, other character sets defined by IANA for the "text" tree may be used with the "charset" parameter.
- Line breaks: Implementers should note that although this specification uses CRLF to denote line breaks, some implementations may use other values.
- Tolerance: When processing CSV files, implementers should follow the RFC 793 [8] principle: "be conservative in what you do, be liberal in what you accept from others."