CVS Files

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

Instruction

Spreadsheets weren’t designed as databases. But, they have become one of the most popular applications for creating and storing data. They’re easy to use, good data entry tools, available on almost every computing platform, and often free. They enable users to perform calculations, analyze, and visualize data. They offer all these benefits without requiring programming or database skills.

Basic CSV File Reading

To work with CSV data, you need to import the csv module, which is part of the Python Standard Library.

import csv

with open("some-file.csv", "r") as file:
  reader = csv.reader(file)
  for row in reader:
    print(row)
browser,vendor,year_appeared
Chrome,Google,2008
Safari,Apple,2003
Edge,Microsoft,2015
['browser', 'vendor', 'year_appeared']
['Chrome', 'Google', '2008']
['Safari', 'Apple', '2003']
['Edge', 'Microsoft', '2015']

Basic CSV File Writing

Reading a CSV file involves creating a file object and then passing it to csv.reader(). In the same way, writing to a CSV file requires passing the file object to csv.writer(). This creates a CSV writer object that writes to the file in CSV format.

one,two
three,four
import csv

with open("some-other-file.csv", "w") as file:
  writer = csv.writer(file)

  # Write header row
  writer.writerow([
    "browser",
    "vendor",
    "year_appeared"
  ])

  # Write data rows
  data_rows = [
    ["Chrome", "Google", 2008],
    ["Safari", "Apple", 2003],
    ["Edge", "Microsoft", 2015]
  ]
  writer.writerows(data_rows)
browser,vendor,year_appeared
Chrome,Google,2008
Safari,Apple,2003
Edge,Microsoft,2015

Advanced CSV File Reading with DictReader

While each row in a CSV file can be treated as a list of values, they can also be converted into dictionaries, where the field names are the keys. This approach makes the data easier to read and understand. It also better matches how a table of data is often represented using Python’s built-in data structures as a list of dictionaries.

import csv

with open("some-other-file.csv", "r") as file:
  reader = csv.DictReader(file)
  for row in reader:
    print(row)
browser,vendor,year_appeared
Chrome,Google,2008
Safari,Apple,2003
Edge,Microsoft,2015
{'browser': 'Chrome', 'vendor': 'Google', 'year_appeared': '2008'}
{'browser': 'Safari', 'vendor': 'Apple', 'year_appeared': '2003'}
{'browser': 'Edge', 'vendor': 'Microsoft', 'year_appeared': '2015'}

Advanced CSV File Writing with DictWriter

Just as reading a CSV file involves using a DictReader object, writing to a CSV file involves using a DictWriter object:

import csv

field_names = [
  "browser",
  "vendor",
  "year_appeared"
]
data_rows = [
  {
    "browser": "Chrome",
    "vendor": "Microsoft",
    "year_appeared": 2008,
  },
  {
    "browser": "Safari",
    "vendor": "Apple",
    "year_appeared": 2003,
  },
  {
    "browser": "Edge",
    "vendor": "Microsoft",
    "year_appeared": 2015,
  },
]

with open("yet-one-more-file.csv", 'w') as file:
  writer = csv.DictWriter(file, fieldnames=field_names)

  writer.writeheader()
  for row in data_rows:
    writer.writerow(row)
See forum comments
Download course materials from Github
Previous: File Handling Demo Next: CSV Files Demo