The csv Module
Reference: https://docs.python.org/3/library/csv.html.
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:
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:
. Thenewline
parameter should fix this behavior.
Reading CSV Files
Process the "teams.csv" file into some Python dictionaries by running this script:
Last updated
Was this helpful?