Skip to content

Instantly share code, notes, and snippets.

@khornberg
Created October 27, 2022 17:57
Show Gist options
  • Save khornberg/2830e73018e9d9176e2af2ba4a31eff3 to your computer and use it in GitHub Desktop.
Save khornberg/2830e73018e9d9176e2af2ba4a31eff3 to your computer and use it in GitHub Desktop.
Reading a CSV file
"""
More documenation about the csv moodule at https://docs.python.org/3/library/csv.html
"""
import csv
# the path to the file is relative to where you execute the python command
# both this file and the test.csv file should be in the same directory/folder
# you should run the command `python example.py` (without the backticks `) from the directory that contains example.py
with open('test.csv') as csvfile:
row_reader = csv.reader(csvfile)
# row_reader reads each row of your csv file
for row in row_reader:
print(row)
"""
Outputs
['col-a', 'col-b']
['value-a', 'value-b']
"""
col-a col-b
value a value-b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment