Python for AI: A Crash Course

Nov 16 2024 · Python 3.12, JupyterLab 4.2.4

Lesson 04: Working with Local Data (File Operations & Data Handling)

CSV Files Demo

Episode complete

Play next episode

Next

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

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

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

Unlock now

00:00In this demo, you’ll read and write CSV files. Just as you did in the demo for text files, you’ll first write some code to write a CSV file and then write code to read it.

00:10Write a new CSV file. Open the working-with-files-starter.ipynb notebook and scroll to the Working with CSV Files section.

import csv

with open('operating-systems.csv', 'w') as file:
  writer = csv.writer(file)

  # Write header row
  writer.writerow(['operating_system', 'creator', 'year'])

  # Write data rows
  data_rows = [
    ["MS-DOS", "Microsoft", 1981],
    ["macOS", "Apple", 1984],
    ["Windows", "Microsoft", 1985],
    ["Linux", "Linus Torvalds", 1991],
  ]
  writer.writerows(data_rows)
operating_system,creator,year
MS-DOS,Microsoft,1981
macOS,Apple,1984
Windows,Microsoft,1985
Linux,Linus Torvalds,1991

01:22Now, write some code to read the CSV file that the previous code wrote. Run the following in a new code cell:

with open("operating-systems.csv", "r") as file:
  reader = csv.reader(file)
  for row in reader:
    print(row)
['operating_system', 'creator', 'year']
['MS-DOS', 'Microsoft', '1981']
['macOS', 'Apple', '1984']
['Windows', 'Microsoft', '1985']
['Linux', 'Linus Torvalds', '1991']

01:54Use DictReader to read operating-systems.csv as a collection of dictionaries. This is possible because the file’s first line contains the column headers’ names.

with open("operating-systems.csv", "r") as file:
  reader = csv.DictReader(file)
  for row in reader:
    print(row)
{'operating_system': 'MS-DOS', 'creator': 'Microsoft', 'year': '1981'}
{'operating_system': 'macOS', 'creator': 'Apple', 'year': '1984'}
{'operating_system': 'Windows', 'creator': 'Microsoft', 'year': '1985'}
{'operating_system': 'Linux', 'creator': 'Linus Torvalds', 'year': '1991'}

02:26Finally, write the CSV version of the “programming languages” file you wrote for the text files demo. This will use DictReader’s counterpart, DictWriter, to write the file and the programming_languages list as the data source.

field_names = [
  "name",
  "creator",
  "year_appeared",
  "site_url",
  "active",
]
with open("programming languages.csv", 'w') as file:
  writer = csv.DictWriter(file, fieldnames=field_names)

  writer.writeheader()
  for row in programming_languages:
    writer.writerow(row)
with open("programming languages.csv", "r") as file:
  reader = csv.DictReader(file)
  for row in reader:
    print(row)
{'name': 'Python', 'creator': 'Guido van Rossum', 'year_appeared': '1991', 'site_url': 'https://www.python.org/', 'active': 'True'}
...
See forum comments
Cinema mode Download course materials from Github
Previous: CVS Files Next: JSON Files