Skip to content

Processing CSV Data with the csv Module

csv_read.py
import csv
 
with open("input.csv", newline="", encoding="utf-8") as f:
    reader = csv.DictReader(f)
    for row in reader:
        print(row)
csv_write.py
import csv
 
rows = [
    {"name": "Alice", "score": 95},
    {"name": "Bob", "score": 88},
]
 
with open("out.csv", "w", newline="", encoding="utf-8") as f:
    writer = csv.DictWriter(f, fieldnames=["name", "score"])
    writer.writeheader()
    writer.writerows(rows)
  • strip whitespace
  • handle missing values
  • normalize casing

Exercise 2 – Join Paths with os.path.join

Section titled “Exercise 2 – Join Paths with os.path.join”

pch.coffeeTagline

pch.coffeeCta

pch.feedbackHeading

pch.feedbackSubheading