Use the csv module to process data stored in Comma Separated Values (CSV) format.
To setup these examples, create a new directory on your Desktop called "csv-mgmt" and navigate there from your command line. Create two Python scripts in that directory called "write_teams.py" and "read_teams.py", and place inside contents from the following sections, respectively.
Writing CSV Files
Write some Python dictionaries to a CSV file called "teams.csv" by running this script:
# csv-mgmt/write_teams.pyimport csvcsv_file_path ="teams.csv"# a relative filepathwithopen(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"})
pythonwrite_teams.py#> city,name#> New York,Yankees#> New York,Mets#> Boston,Red Sox#> New Haven,Ravens
FYI: if you're on Windows, this CSV writing approach may insert a blank row between each real row you're trying to write. To remedy this, change your file open command to: with open(csv_file_path, "w", newline="") as csv_file:. The newline parameter should fix this behavior.
Reading CSV Files
Process the "teams.csv" file into some Python dictionaries by running this script:
# csv-mgmt/read_teams.pyimport csvcsv_file_path ="teams.csv"# a relative filepathwithopen(csv_file_path, "r")as csv_file:# "r" means "open the file for reading" reader = csv.DictReader(csv_file)# assuming your CSV has headers# reader = csv.reader(csv_file) # if your CSV doesn't have headersfor row in reader:print(row["city"], row["name"])
pythonread_teams.py#> New York Yankees#> New York Mets#> Boston Red Sox#> New Haven Ravens