Intro to Reading CSV files with Python

2020-11-03

Reading CSV files is a common task you have to perform in Python. Let's take a look at how we can read CSV files.

With Standard Python

Let's start with reading a CSV file with basic python. We can do this using the with and open keyword and then loop through the contents.

For example, let's say we have a CSV file that looks like this, called example.csv.

a,b,c
1,2,3
4,5,6
filename = 'example.csv'

with open(filename, 'r') as csvfile:
    for row in csvFile:
        print(row)

Here we use the with, open, and as keyword to open a file. This tells Python to read the file located at our filename variable. We use these keywords so Python handles closing the file when we are done. Notice that when using open we added r which means to open the file for reading. There are other options we will look at in the future.

Once, the file is open, we can loop through our new variable, csvfile and this gives us the lines in the file. However, this will not parse the file, so in our example when we print row, we will see the full line, i.e. 1,2,3. We can parse that with the split function.

filename = 'example.csv'

with open(filename, 'r') as csvfile:
    for row in csvFile:
        parsedRow = row.split(",")
        print(parsedRow) # [1, 2, 3]

There we go! Now we have a list of the items in our rows stored in the variable parseRow.

With CSV module

Okay, the above works well, but sometimes CSVs have weird characters. Also, remembering to split our rows adds some extra logic that we don't want to repeat every time we read a CSV file. For this, we can use the csv standard library.

import csv

filename = 'example.csv'

with open(filename, 'r') as csvfile:
    reader = csv.reader(csvfile)
    for row in reader:
        print(row) # [1, 2, 3]

Here, we added import csv to the top to pull in the csv library. Then we added reader = csv.reader(csvfile) which will automatically parse our CSV file and turn our lines into lists (like we did by using split).

Conclusion

That's all for this lesson! There are many more options for handling CSV files that we can explore in the future. If you want to learn more, check out the Python official docs here: https://docs.python.org/3/library/csv.html.

GoTea - KoalaTea