The csv Module
Writing CSV Files
# csv-mgmt/write_teams.py
import csv
csv_file_path = "teams.csv" # a relative filepath
with open(csv_file_path, "w") as csv_file: # "w" means "open the file for writing"
writer = csv.DictWriter(csv_file, fieldnames=["city", "name"])
writer.writeheader() # uses fieldnames set above
writer.writerow({"city": "New York", "name": "Yankees"})
writer.writerow({"city": "New York", "name": "Mets"})
writer.writerow({"city": "Boston", "name": "Red Sox"})
writer.writerow({"city": "New Haven", "name": "Ravens"})Reading CSV Files
Last updated