As an Amazon Associate I earn from qualifying purchases.
Today, you will learn how to read and write CSV files in Python. CSV (comma-separated values) are one of the most widely used types to store table data. To read and write CSV files, we will be using the csv
module from Python.
Reading a CSV
If you simply want to open a CSV and print out the contents in Python, you can do so by running the following code:
import csv
with open('YOUR_CSV.csv', 'r') as file:
reader = csv.reader(file)
for row in reader:
print(row)
Writing a CSV
Using arrays
If you want to write a CSV using arrays:
- Import the
csv
module. - Define an array for the header.
- Define an array of arrays for the rows.
- Create a new CSV file in Python using
csv.writer.
- Write the header with
writerow
. - Write the rows using
writerows
.
import csv
header = ['PAIR', 'PRICE']
rows = [ ['USDCAD', 1.27],
['GBPUSD', 1.38],
['EURUSD', 1.19]]
with open("forex_prices.csv", 'w', newline='') as file:
csvwriter = csv.writer(file)
csvwriter.writerow(header)
csvwriter.writerows(rows)
Using dictionaries
Saving an array of dictionaries to a CSV file is similar to saving an array of rows:
- Import the
csv
module. - Define the header of your table using an array.
- Define the array of dictionaries (each row).
- Create a new CSV file using
csv.DictWriter
passing in the file and setfieldnames = header
. - Call
writeheader()
to write the header. - Call
writerows
and pass the array of dictionaries.
import csv
header = ['PAIR', 'PRICE']
rows_dict =[{'PAIR': 'USDCAD', 'PRICE': 1.27},
{'PAIR': 'GBPUSD', 'PRICE': 1.38},
{'PAIR': 'EURUSD', 'PRICE': 1.19}]
with open("forex_prices.csv", 'w', newline='') as file:
csvwriter = csv.DictWriter(file, fieldnames = header)
csvwriter.writeheader()
csvwriter.writerows(rows_dict)
Quoting
When reading and writing a CSV, you can specify what type of quoting you want. This means that if you want to put quotes around all data, or only certain data you can do this using quoting. There are 4 types of quoting constants in the CSV module which can be passed to csv.writer
and csv.DictWriter
:
QUOTE_NONE
– Quotes nothing.QUOTE_NONNUMERIC
– Quotes anything that is not an int or floatQUOTE_MINIMAL
– Quotes every field that contains a special characterQUOTE_ALL
– Quotes every field
Example
If you want to quote every field, you would create a new csv.writer
as follows:
import csv
header = ['PAIR', 'PRICE']
rows = [ ['USDCAD', 1.27],
['GBPUSD', 1.38],
['EURUSD', 1.19]]
with open("forex_prices.csv", 'w', newline='') as file:
csvwriter = csv.writer(file, quoting=csv.QUOTE_ALL)
csvwriter.writerow(header)
csvwriter.writerows(rows)
Output:
"PAIR","PRICE"
"USDCAD","1.27"
"GBPUSD","1.38"
"EURUSD","1.19"
As you can see above, everything has been quoted. Let’s see how this differs from using QUOTE_NONNUMERIC
:
import csv
header = ['PAIR', 'PRICE']
rows = [ ['USDCAD', 1.27],
['GBPUSD', 1.38],
['EURUSD', 1.19]]
with open("forex_prices.csv", 'w', newline='') as file:
csvwriter = csv.writer(file, quoting=csv.QUOTE_NONNUMERIC)
csvwriter.writerow(header)
csvwriter.writerows(rows)
Output:
"PAIR","PRICE"
"USDCAD",1.27
"GBPUSD",1.38
"EURUSD",1.19
As you can see, Only the PAIR
field has been quoted as it is not an int or float.
Dialect
The csv
module offers a concept called dialects. A dialect allows you to modify the format of reading/writing a CSV file. For instance, if I wanted to use ‘;’ instead of ‘,’ as my delimiter, I could create a dialect for this.
Start by defining your dialect name, specifying the delimiter parameter as ‘;’ and registering it:
csv.register_dialect('semi-colon', delimiter=';')
When we call csv.writer
or csv.reader
you can specify the dialect by using the dialect name you defined above. In my case this is ‘semi-colon’:
csvwriter = csv.writer(file, dialect='semi-colon')
Example
import csv
header = ['PAIR', 'PRICE']
rows = [ ['USDCAD', 1.27],
['GBPUSD', 1.38],
['EURUSD', 1.19]]
csv.register_dialect('semi-colon', delimiter=';')
with open("forex_prices.csv", 'w', newline='') as file:
csvwriter = csv.writer(file, dialect='semi-colon')
csvwriter.writerow(header)
csvwriter.writerows(rows)
Now when this code is run, instead of the fields being separated by ‘,’ – they will be separated by ‘;’
PAIR;PRICE
USDCAD;1.27
GBPUSD;1.38
EURUSD;1.19
Dialect Parameters
A comprehensive list of every parameter you can use in a Dialect and their default values:
Parameter | Description | Default Value |
delimiter | The delimiter used to separate values. | , |
doublequote | If set to true, it will use quotes to escape quotes. Otherwise it will use escape characters. | True |
escapechar | The character used for escaping. Disabled if None. | None |
lineterminator | The delimiter used for the end of a record. | \r\n |
quotechar | The character used as a delimiter for escaped fields. | “ |
quoting | Specifies the fields should be quotedQUOTE_NONE – Quotes nothing.QUOTE_NONNUMERIC – Quotes anything that is not an int or floatQUOTE_MINIMAL – Quotes every field that contains a special characterQUOTE_ALL – Quotes every field | csv.QUOTE_MINIMAL |
skipinitialspace | Strip spacing after the field delimiter. | True |
strict | Raises an exception if the CSV is malformed. | False |
That’s all for how to read and write CSV files in Python! As always, if you have any questions or comments please feel free to post them below. Additionally, if you run into any issues please let me know.
If you’re interested in learning Python I highly recommend this book. In the first half of the book, you”ll learn basic programming concepts, such as variables, lists, classes, and loops, and practice writing clean code with exercises for each topic. In the second half, you”ll put your new knowledge into practice with three substantial projects: a Space Invaders-inspired arcade game, a set of data visualizations with Python”s handy libraries, and a simple web app you can deploy online. Get it here.
Check out these other Python tutorials!