The CSV file format
A comma-separated values, CSV, file is used to store data structured in a table or list form. Each line in the CSV file corresponds to a row in the table. Within a line, fields are separated by commas, each field belonging to one table column. Since it is a common and simple file format, CSV files are often used for moving tabular data between two different computer programs.
Basic Rules
CSV is a delimited data format that has fields /columns separated by a comma. The field delimiter is placed between each field of a record. Fields that contain a special character such as comma, newline, or double quote, must be enclosed in double quotes.
The first record in a CSV file may contain column names in each of the fields:
-
Year,Make,Model
-
2008,Ford,Mustang
-
2007,Chevrolet,Corvette
Any character or sequence of characters may be used to separate the values, but the most common delimiters are the comma, tab, and colon. The vertical bar, also referred to as pipe, and space are also sometimes used.
Theoretically, when using another character than the comma, the file would be called a DSV (delimiter-separated values) file.
Examples
Year | Make | Model | Options | Price |
---|---|---|---|---|
1997 | Ford | Mustang | ac, abs | 18000.00 |
1999 | Chevy |
Corvette |
ac, abs |
25000.00 |
2008 | Chevy | Corvette | C06 |
50000.00 |
1996 | Jeep |
Grand Cherokee |
MUST SELL! ac, moon roof, loaded |
4799.00 |
The above table of data may be represented in CSV format as follows:
-
Year;Make;Model;Options;Price
-
1997;Ford;Mustang;ac, abs;18000.00
-
1999;Chevy;Corvette;ac, abs;25000.00
-
2008;Chevy;Corvette;C06;50000.00
-
1996;Jeep;Grand Cherokee;MUST SELL!;4799.00
-
;;;ac, moon roof, loaded;
This CSV example illustrates that:
-
Fields that contain the delimiter (commas, double-quotes, or line-breaks) must be quoted;
-
A quote within a field must be accompanied with an additional quote immediately preceding the literal quote;
-
A quote and a delimiter (comma within a field must be accompanied with an extra additional quote preceding the literal quote.