Zum Inhalt springen

CSV (Comma-Separated Values)

Aus Kirla Wiki

Comma-Separated Values (CSV, englisch für „durch Kommata getrennte Werte“) ist ein Textformat zur Speicherung tabellarischer Daten, bei dem die Datensätze in Zeilen und die Felder innerhalb eines Datensatzes durch Kommata getrennt stehen. Als Medientyp ist es unter text/csv registriert; die gemeinsame Formatdefinition wurde im Standarddokument RFC 4180 (Volltext) dokumentiert und im Januar 2014 durch RFC 7111 (Volltext) um URI-Fragment-Bezeichner erweitert. S1 S2 S3 S14

Datei:Example CSV file.png
Eine CSV-Datei mit Firmennamen und Ansprechpartnern

Geschichte und Dokumentation

CSV ist seit einiger Zeit zum Austausch und zur Umwandlung von Daten zwischen Tabellenkalkulationsprogrammen im Gebrauch; erstaunlicherweise sei dieses sehr verbreitete Format nie offiziell dokumentiert gewesen, stellt RFC 4180 fest: „The comma separated values format (CSV) has been used for exchanging and converting data between various spreadsheet programs for quite some time. Surprisingly, while this format is very common, it has never been formally documented.“ S2 Während die IANA-MIME-Registrierungsliste eine Anmeldung für „text/tab-separated-values“ enthielt, sei nie ein MIME-Typ für CSV bei der IANA angemeldet worden. S2

Die Zusammenfassung von RFC 4180 hält fest: „This RFC documents the format used for Comma-Separated Values (CSV) files and registers the associated MIME type \“text/csv\”.“ S1 Die MIME-Anmeldung selbst erfolgte durch die in Abschnitt 3 des Dokuments bereitgestellte Anmeldung; die IANA habe den Medientyp text/csv mit dieser Anmeldung registriert: „The IANA has registered the MIME type \“text/csv\” using the application provided in Section 3 of this document.“ S12

RFC 4180 vom Oktober 2005 ist als Informational-Dokument gekennzeichnet und bezieht sich selbst ausdrücklich auf den Status: „This memo provides information for the Internet community. It does not specify an Internet standard of any kind.“ (RFC 4180, Status of This Memo) S35

RFC 7111 vom Januar 2014 aktualisiert diesen Medientyp: „This memo updates the text/csv media type defined in RFC 4180 [RFC4180] by defining URI fragment identifiers for text/csv MIME entities.“ S15

Formatdefinition nach RFC 4180

RFC 4180 hält fest, dass es zwar diverse Spezifikationen und Implementierungen für das CSV-Format gebe, aber keine formale Spezifikation existiere, was eine große Bandbreite von Interpretationen ermögliche; der Abschnitt dokumentiere das Format, dem die meisten Implementierungen zu folgen scheinen: „While there are various specifications and implementations for the CSV format (for ex. [4], [5], [6] and [7]), there is no formal specification in existence, which allows for a wide variety of interpretations of CSV files. This section documents the format that seems to be followed by most implementations:“ S21

  • „Each record is located on a separate line, delimited by a line break (CRLF).“ S4
  • „The last record in the file may or may not have an ending line break.“ S5
  • „There maybe an optional header line appearing as the first line of the file with the same format as normal record lines.“ — so der Wortlaut von RFC 4180, einschließlich der Schreibweise „maybe“ anstelle von „may be“; es folgt die Bedingung „This header will contain 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).“ S6
  • „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.“ S7
  • „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.“ S8
  • „Fields containing line breaks (CRLF), double quotes, and commas should be enclosed in double-quotes.“ S9
  • „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.“ S10

Der Dokumentation liegt folgende ABNF-Grammatik bei: „file = [header CRLF] record *(CRLF record)“; „header = name *(COMMA name)“; „record = field *(COMMA field)“; „field = (escaped / non-escaped)“; „escaped = DQUOTE *(TEXTDATA / COMMA / CR / LF / 2DQUOTE) DQUOTE“; „non-escaped = *TEXTDATA“ S11

file = [header CRLF] record *(CRLF record)
header = name *(COMMA name)
record = field *(COMMA field)
field = (escaped / non-escaped)
escaped = DQUOTE *(TEXTDATA / COMMA / CR / LF / 2DQUOTE) DQUOTE
non-escaped = *TEXTDATA

Das folgende Beispiel folgt dem Beispielaufbau aus RFC 4180, Abschnitt 2 — oben die CSV-Datei, darunter ihre tabellarische Darstellung: S31

field name,field name,field name CRLF
aaa,bbb,ccc CRLF
xxx,yyy,zzz CRLF
Beispieldatei nach RFC 4180, Abschnitt 2, als Tabelle gelesen
field name field name field name
aaa bbb ccc
xxx yyy zzz

MIME-Typ und Parameter

Der Medientyp wurde als text/csv mit den Pflichtparametern „none“ und den optionalen Parametern „charset, header“ registriert. S3 Zur Kodierung heißt es in RFC 4180: „As per section 4.1.1. of RFC 2046 [3], this media type uses CRLF to denote line breaks. However, implementors should be aware that some implementations may use other values.“ S13

Merkmale der IANA-Registrierung von text/csv
Merkmal Wert
Pflichtparameter none
Optionale Parameter charset, header
Magic number(s) none
File extension(s) CSV
Macintosh File Type Code(s) TEXT
Intended usage COMMON
Änderungskontrolle IESG

Die zusätzlichen Angaben stammen aus der IANA-Anmeldung in RFC 4180, Abschnitt 3; die beabsichtigte Nutzung ist „COMMON“, Autor und Änderungskontrolle liegt bei der IESG. S23 S28 S29

RFC 7111 ergänzt die Parameterbedeutung: „The \“charset\” parameter specifies the charset employed by the CSV content. In accordance with RFC 6657 [RFC6657], the charset parameter SHOULD be used, and if it is not present, UTF-8 SHOULD be assumed as the default“. S19 Zum header-Parameter: „The \“header\” parameter indicates the presence or absence of the header line. Valid values are \“present\” or \“absent\”. Implementors choosing not to use this parameter must make their own decisions as to whether the header line is present or absent.“ S19 S6

URI-Fragment-Bezeichner nach RFC 7111

RFC 7111 definiert URI-Fragment-Bezeichner für text/csv: „This memo defines URI fragment identifiers for text/csv MIME entities. These fragment identifiers make it possible to refer to parts of a text/csv MIME entity identified by row, column, or cell. Fragment identification can use single items or ranges.“ S14 Da die Änderung der Medientyp-Registrierung die Zustimmung der IESG erforderte, sei sie nach Konsultation der IETF-Gemeinschaft genehmigt worden. S15

RFC 7111 spezifiziert die Methoden: „This memo specifies fragment identification using the following methods: \“row\” for row selections, \“col\” for columns selections, and \“cell\” for cell selections.“ S16

Beispiele für Fragment-Bezeichner auf eine Beispieltabelle
Fragment Beispiel-URL Auswahl
#row=4 example.com/data.csv#row=4 vierte Zeile
#col=2 example.com/data.csv#col=2 zweite Spalte
#cell=4,1 example.com/data.csv#cell=4,1 Zelle in Zeile 4, Spalte 1

In der Syntaxdefinition werden „rowsel“, „colsel“ und „cellsel“ als gleichwertige Alternativen verwendet: „csv-fragment = rowsel / colsel / cellsel“ S17

Zeilen und Spalten werden ab eins gezählt: „Rows and columns in CSV are counted from one.“ S18 Das Sternchen steht für die letzte Zeile oder Spalte. S32 S33

Mehrfachauswahl: „Row, column, and cell selections can make more than one selection, in which case the individual selections are separated by semicolons. In these cases, the resulting fragment may be a disjoint fragment“. S22

Verarbeitung und Fehlerverhalten

Zur Verarbeitung von Fehlern: „If a fragment identifier contains a syntax error (i.e., does not conform to the syntax specified in Section 3), then it MUST be ignored by clients. Clients MUST NOT make any attempt to correct or guess fragment identifiers. Syntax errors MAY be reported by clients.“ S20

Weitere Verarbeitungsregeln: „If single selections refer to non-existing rows or columns (i.e., beyond the size of the CSV), they MUST be ignored.“ und „If ranges extend beyond the size of the CSV (by extending to rows or columns beyond the size of the CSV), they MUST be interpreted to only extend to the actual size of the CSV.“ S18

Bei inversen Bereichen: „If selections of ranges of rows, ranges of columns, or ranges of cells are specified in a way so that they select \“inversely\” (i.e., \“#row=10-5\” or \“#cell=10,10-5,5\”), they MUST be ignored.“ S18

Jede Spezifikation wird unabhängig verarbeitet: „Each specification of an identified region is processed independently, and ignored specifications (because of reasons listed in the previous paragraphs) do not cause the whole fragment identifier to fail, they just mean that this single specification is ignored.“ S18

Sicherheit und Abgrenzung

RFC 4180 stuft das Format als passiven Text ein: „CSV files contain passive text data that should not pose any risks. However, it is possible in theory that malicious binary data may be included in order to exploit potential buffer overruns in the program processing CSV data. Additionally, private data may be shared via this format (which of course applies to any text data).“ S25

RFC 7111 hält fest: „Text/csv consists of nothing but passive text data that should not pose any direct risks. However, it is possible that malicious data may be included in order to exploit buffer overruns or other bugs in the program processing the text/csv data.“ Zusätzlich: „The text/csv format provides no confidentiality or integrity protection, so if such protections are needed, they must be supplied externally.“ S26

Abgrenzung gegenüber text/tab-separated-values: Die IANA-Liste enthielt laut RFC 4180 eine Anmeldung für „text/tab-separated-values“, aber nie eine für CSV selbst. S2

English version (United States)

Diese Abschnitt ist die englische Sprachfassung des Artikels in der US-Variante des Amerikanischen Englisch.

Comma-Separated Values (CSV) is a text format for storing tabular data in which records are stored as lines and the fields of a record are separated by commas. As a media type it is registered as text/csv; its common format definition was documented in RFC 4180 (full text) and updated in January 2014 by RFC 7111 (full text), which defines URI fragment identifiers for text/csv. S1 S2 S3 S14

History and documentation

CSV has been used for exchanging and converting data between spreadsheet programs for quite some time; surprisingly, while this format is very common, it has never been formally documented, as RFC 4180 states: “The comma separated values format (CSV) has been used for exchanging and converting data between various spreadsheet programs for quite some time. Surprisingly, while this format is very common, it has never been formally documented.” S2 While the IANA MIME registration list contained a registration for “text/tab-separated-values”, a MIME type for CSV itself was never registered with the IANA. S2

The abstract of RFC 4180 states: “This RFC documents the format used for Comma-Separated Values (CSV) files and registers the associated MIME type \”text/csv\”.“ S1 The MIME registration was provided in Section 3 of the document; the IANA registered the media type text/csv using this application: “The IANA has registered the MIME type \”text/csv\” using the application provided in Section 3 of this document.” S12

RFC 4180 from October 2005 is marked as an Informational document: “This memo provides information for the Internet community. It does not specify an Internet standard of any kind.” (RFC 4180, Status of This Memo) S35

RFC 7111 from January 2014 updates this media type: “This memo updates the text/csv media type defined in RFC 4180 [RFC4180] by defining URI fragment identifiers for text/csv MIME entities.” S15

Format definition per RFC 4180

RFC 4180 states that while there are various specifications and implementations for the CSV format, there is no formal specification in existence, which allows for a wide variety of interpretations of CSV files; its section documents the format that seems to be followed by most implementations: “While there are various specifications and implementations for the CSV format (for ex. [4], [5], [6] and [7]), there is no formal specification in existence, which allows for a wide variety of interpretations of CSV files. This section documents the format that seems to be followed by most implementations:” S21

  • “Each record is located on a separate line, delimited by a line break (CRLF).” S4
  • “The last record in the file may or may not have an ending line break.” S5
  • “There maybe an optional header line appearing as the first line of the file with the same format as normal record lines.” — the presence or absence of the header line should be indicated via the optional \”header\” parameter of this MIME type. S6
  • “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.” S7
  • “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.” S8
  • “Fields containing line breaks (CRLF), double quotes, and commas should be enclosed in double-quotes.” S9
  • “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.” S10

The documentation includes the following ABNF grammar: S11

file = [header CRLF] record *(CRLF record)
header = name *(COMMA name)
record = field *(COMMA field)
field = (escaped / non-escaped)
escaped = DQUOTE *(TEXTDATA / COMMA / CR / LF / 2DQUOTE) DQUOTE
non-escaped = *TEXTDATA

The following example follows the example layout from RFC 4180, Section 2 — above the CSV file, below its tabular reading: S31

field name,field name,field name CRLF
aaa,bbb,ccc CRLF
xxx,yyy,zzz CRLF
Example file after RFC 4180, Section 2, read as a table
field name field name field name
aaa bbb ccc
xxx yyy zzz

MIME type and parameters

The media type was registered as text/csv with mandatory parameters “none” and optional parameters “charset, header”. S3 On encoding, RFC 4180 says: “As per section 4.1.1. of RFC 2046 [3], this media type uses CRLF to denote line breaks. However, implementors should be aware that some implementations may use other values.” S13

IANA registration details of text/csv
Item Value
Mandatory parameters none
Optional parameters charset, header
Magic number(s) none
File extension(s) CSV
Macintosh File Type Code(s) TEXT
Intended usage COMMON
Change control IESG

The additional information comes from the IANA registration in Section 3 of RFC 4180; intended usage is \”COMMON\”, and change control is with the IESG. S23 S28 S29

RFC 7111 adds the parameter meanings: “The \”charset\” parameter specifies the charset employed by the CSV content. In accordance with RFC 6657 [RFC6657], the charset parameter SHOULD be used, and if it is not present, UTF-8 SHOULD be assumed as the default”. S19 On the header parameter: “The \”header\” parameter indicates the presence or absence of the header line. Valid values are \”present\” or \”absent\”. Implementors choosing not to use this parameter must make their own decisions as to whether the header line is present or absent.” S19 S6

URI fragment identifiers per RFC 7111

RFC 7111 defines URI fragment identifiers for text/csv: “This memo defines URI fragment identifiers for text/csv MIME entities. These fragment identifiers make it possible to refer to parts of a text/csv MIME entity identified by row, column, or cell. Fragment identification can use single items or ranges.” S14 Because the change to the media type registration required the approval of the IESG, it was approved after consultation of the IETF community. S15

RFC 7111 specifies the methods: “This memo specifies fragment identification using the following methods: \”row\” for row selections, \”col\” for columns selections, and \”cell\” for cell selections.” S16

Examples of fragment identifiers applied to a sample table
Fragment Example URL Selection
#row=4 example.com/data.csv#row=4 fourth row
#col=2 example.com/data.csv#col=2 second column
#cell=4,1 example.com/data.csv#cell=4,1 cell in row 4, column 1

In the syntax definition, “rowsel”, “colsel” and “cellsel” are used as equivalent alternatives: “csv-fragment = rowsel / colsel / cellsel” S17

Rows and columns in CSV are counted from one: “Rows and columns in CSV are counted from one.” S18 The asterisk denotes the last row or column. S32 S33

Multiple selections: “Row, column, and cell selections can make more than one selection, in which case the individual selections are separated by semicolons. In these cases, the resulting fragment may be a disjoint fragment”. S22

Processing and error behaviour

On error handling: “If a fragment identifier contains a syntax error (i.e., does not conform to the syntax specified in Section 3), then it MUST be ignored by clients. Clients MUST NOT make any attempt to correct or guess fragment identifiers. Syntax errors MAY be reported by clients.” S20

Further processing rules: “If single selections refer to non-existing rows or columns (i.e., beyond the size of the CSV), they MUST be ignored.” and “If ranges extend beyond the size of the CSV (by extending to rows or columns beyond the size of the CSV), they MUST be interpreted to only extend to the actual size of the CSV.” S18

For inverse ranges: “If selections of ranges of rows, ranges of columns, or ranges of cells are specified in a way so that they select \”inversely\” (i.e., \”#row=10-5\” or \”#cell=10,10-5,5\”), they MUST be ignored.” S18

Each specification is processed independently: “Each specification of an identified region is processed independently, and ignored specifications (because of reasons listed in the previous paragraphs) do not cause the whole fragment identifier to fail, they just mean that this single specification is ignored.” S18

Security and delimitation

RFC 4180 classifies the format as passive text: “CSV files contain passive text data that should not pose any risks. However, it is possible in theory that malicious binary data may be included in order to exploit potential buffer overruns in the program processing CSV data. Additionally, private data may be shared via this format (which of course applies to any text data).” S25

RFC 7111 states: “Text/csv consists of nothing but passive text data that should not pose any direct risks. However, it is possible that malicious data may be included in order to exploit buffer overruns or other bugs in the program processing the text/csv data.” Additionally: “The text/csv format provides no confidentiality or integrity protection, so if such protections are needed, they must be supplied externally.” S26

Delimitation against text/tab-separated-values: according to RFC 4180, the IANA list contained a registration for “text/tab-separated-values”, but never one for CSV itself. S2

Quellen