Character-Seperated Value (CSV) Files

Terminology

A CSV file is made up of many records, where a record is a single entire line in the file. Each new line is signified with the record delimiter (typically a carriage return and line feed character, CRLF or \r\n). A record is made up of many fields, each which a seperated by the field delimiter (typicallay a comma or a tab character).

General Rules

  1. Each record is located on a different line, and each line is seperated by the carriage return and line break characters (the record delimiter).
  2. The last record in the file may or may not have a record delimiter at the end of it.
    xxx,yyy,zzz CRLF (record delimiter on last line)
    or
    xxx,yyy,zzz (no record delimiter on last line)
  3. There is an optional header record at the top of the file (the first line), with the same format as the rest of the records. The header contains names/descriptions of the fields in the file.
    Field1Name, Field2Name, Field3Name
    aaa,bbb,ccc
    xxx,yyy,zzz
  4. White-space (space and tab characters) are considered part of the field, and shall be preserved (i.e. not stripped) when parsing CSV files.

Standards

There is no “official” standard for CSV files, however, RFC 4180 stands as the defacto standard (it formally registers the MIME type “text/csv”.

Posted: April 9th, 2014 at 8:46 am
Last Updated on: April 9th, 2014 at 9:16 am