Python CSV Files Tutorial

Reading CSV Files to lists

Reading CSV Files into Python lists

Good for:

  • Looping through the entire list of rows in the file and doing something for each row.
  • Finding the highest, lowest and average for items in CSV rows.

YouTube Blocked? Try the Google Drive Version

Code Example

import csv

f = open('mycsvfile.csv','r')
reader = csv.reader()

items = []
for row in reader:
     items.append(row)

Reading CSV files to Dictionaries

Reading CSV Files into Python Dictionaries

Good for:

  • Where you just want find information about a specific item in a CSV file.

Can’t access YouTube? Try the Google Drive Version.

Writing to CSV Files

Tutorial Video

YouTube Blocked? Use the Google Drive version instead

Code

import csv
f = open('namesages.csv','a',newline = '')

name = input("What is your name?")
age = input("What is your age?")

row = (name,age)

writer = csv.writer(f)
writer.writerow(row)

f.close() #really important!!!