Using Python CSV #1 — Basic Reading

David W. Agler
3 min readDec 21, 2022

--

Python’s csv module allows you to work with csv files (e.g., Excel spreadsheets). Let’s focus on the absolute basics of reading csv files using this module.

csv.reader

Suppose I have a spreadsheet of student grades. I want to read parts of that spreadsheet in Python. First, we will need to import csv. Next, Python has a csv.reader function. As it’s name implies, it can be used to read csv files. Let’s use it and print the result:

import csv

with open("grades_simple.csv", 'r') as infile:
reader = csv.reader(infile)
print(reader)

The above opens the file in read mode (abbreviated as 'r') as infile. It then defines a variable reader as csv.reader(infile). The csv.reader() is a function that takes infile as its argument. Let’s see what results:

<_csv.reader object at 0x0000019225E7AE60>

Yuck. This is an object that is iterable. That means you can do stuff over the items inside of it. Let’s iterate over it and see what type of thing is inside. We can do this by using a for loop:

with open("grades_simple.csv", 'r') as infile:
reader = csv.reader(infile)
for row in reader:
print(type(row)) # check the type

Here is the result:

<class 'list'>
<class 'list'>
<class 'list'>

OK. So, we have an object that has a bunch of lists inside. Let’s print each list:

with open("grades_simple.csv", 'r') as infile:
reader = csv.reader(infile)
for row in reader:
print(row)

> ['Student,Score']
> ['Soryu, Asuka Langley,91.34']
> ['Ikari, Shinji,80.01']

Ah! This is a spreadsheet of student names and some type of score. Let’s look at some basic things we can do with this information.

Get a single element from a row

Our list consists of student names and grades. Suppose we only wanted the names. We could specify that we only want the first item in each list:

with open("grades_simple.csv", 'r') as infile:
reader = csv.reader(infile)
for row in reader:
student_name = row[0]
print(student_name)

> Student
> Soryu, Asuka Langley
> Ikari, Shinji

Remove the header

The spreadsheet has a header Student but suppose we just want the names, not the header. If you want to remove the header you can use next(reader). This will “skip” over the first item in the iterable.

with open("grades_simple.csv", 'r') as infile:
reader = csv.reader(infile)
next(reader) # returns next item in iterable
for row in reader:
print(row)

Basic operations on the rows

Now that we have a way of reading our spreadsheet, we can interpret the data. Let’s get the average of student grades:

with open("01_basics/grades_simple.csv", 'r') as infile:
reader = csv.reader(infile)
next(reader)
ave = []
for row in reader:
x = float(row[1])
ave.append(x)
print(sum(ave)/len(ave))
> 84.12125

Maybe we want to engage in grade inflation by adding 10 points to everyone’s grade and then let’s check the course average after doing this.

with open("grades_simple.csv", 'r') as infile:
reader = csv.reader(infile)
next(reader)
ave = []
for row in reader:
x = float(row[1])
ave.append(x+10) # add 10 points
print(sum(ave)/len(ave))

> 94.12124999999999

Or, suppose I want a list of students whose grade is below a 75. I want to write to them to see if they need additional help. With some basic logic, we can print out the students with a 75 or below.

with open("grades_simple.csv", 'r') as infile:
reader = csv.reader(infile)
header = next(reader)
danger = []
for row in reader:
x = float(row[1])
if x < 75:
danger.append(row)
print(row)

> ['Fuyutsuki, Kouzou', '27.45']
> ['Aoba, Shigeru', '71.6']

We now have the absolute basics of reading csv files using Python. In the next article, let’s look at writing csv files: Using Python CSV #2 — Basic Writing.

Resources

--

--

David W. Agler

Assistant Teaching Professor - Philosophy. I make logic and philosophy videos at https://www.youtube.com/@LogicPhilosophy