csv

What it is good for?

Read and write comma-separated-value (CSV) files.

The csv module reads and writes nested lists from/to CSV files. You can set a field delimiter, quote character and line terminator character. Note that when reading a line, all columns are in string format.

Installed with Python by default

yes

Example

Write a table with two rows to a CSV file:

  1. import csv
  2. data = [["first", 1, 234],
  3. ["second", 5, 678]]
  4. outfile = open('example.csv', 'w')
  5. writer = csv.writer(outfile, delimiter=';', quotechar='"')
  6. writer.writerows(data)
  7. outfile.close()

Read the file again:

  1. for row in csv.reader(open('example.csv'), delimiter=';'):
  2. print(row)
  3. ['first', '1', '234']
  4. ['second', '5', '678']

Where to learn more?

https://docs.python.org/3/library/csv.html