This is an archived version of the course. Please find the latest version of the course on the main webpage.

Chapter 7: More file handling

CSV files

face Josiah Wang

Let us look at handling another file format that is quite often used: Comma Separated Value (CSV).

A CSV file is a plain text file that structures its data in tabular form. Think spreadsheets.

Generally, CSV files use a comma (,) to separate each data value (hence its name). Other delimiters can also be used: tabs (\t), colons (:), and semi-colons (;).

The first row usually contains the name of the columns. Think of headers in tables. This is usually followed by one record per line.

Here is an example of the content of a CSV file:

name,faculty,department
Alice Smith,Science,Chemistry
Ben Williams,Eng,EEE
Bob Jones,Science,Physics
Andrew Taylor,Eng,Computing