Spring Deals - hours only!Up to 70% off on all courses and bundles.-Close
Introduction
CSV dialects
8. Semicolon as delimiter
Summary

Instruction

Perfect! Before we wrap things up, we want to draw your attention to one particularly popular CSV format that is not available out of the box in Python: a semicolon-delimited dialect. You can create one yourself in the following way:

import csv
csv.register_dialect('semicolon-delimited', delimiter=';')

In the English language, we use dots (.) as decimal separators, and CSV files typically look like this:

23.45,16.78,0.78

However, nearly all countries in Europe and South America use commas (,) instead of periods for decimal delimiters. Thus, CSV files from these countries will typically use a semicolon as the delimiter:

23,45;16,78;0,78

It's good to be aware of this difference when working with lots of CSV data coming from various sources.

Exercise

  1. Open the figures_commas.csv file, which uses commas as field delimiters.
  2. Create a new dialect that uses semicolons as field delimiters.
  3. Save the contents of the file to figures_semicolons.csv using the new dialect. Note: do not change the decimal separator.

Stuck? Here's a hint!

The quickest approach is to create an empty list, use csv.reader, and add each row to the empty list using the listName.append(row) function. Then, create the new dialect and write the contents of the list to the new file.