Tutorial: Reading and Writing .csv Files in Python

Today, we will be learning how to read and write .csv files in Python. This skill can be especially useful for data collection — say you have a series of .csv files and want to join them together, or a .txt file that you need to pull data from to place in a .csv file. Python allows you to avoid copy-and-pasting huge amounts of data in excel, or parsing through a plain-text file manually. In this tutorial, we will learn how to open a .csv file, read its contents, and add more data to it.

Step 1: Setting Up

Import csv Library

Before we do anything, let’s make sure to import Python’s built-in csv library at the top of our script. This will allow us to more easily manipulate the .csv file, and can be accomplished with a simple command:

import csv

There are several options for editing csv files in Python, one of which is “pandas.” This tutorial will focus only on the basic built-in library, but more advanced students might be interested in checking out these other options.

Opening the File

Next, let’s open the file. While many tutorials gloss over this step, the syntax can be rather confusing for beginners. There are several ways to open files, but my preferred method is the with command, along with the open() function. The advantage of this is that it will automatically close the file for us when we are done using it, which is important for security reasons in addition to being good coding practice. We will want to also specify the mode that we are in — this is signified with an “r” for read-only mode, and a “w” for write mode. We’ll open in read mode for now:

import csv

with open('mvp_winners.csv', 'r') as file:
  # Text goes here

As we begin to write our program, all of our code will go within this text block.

Step 2: Reading the File

Here’s what our file (winners of the award for best soccer player in the world by year) looks like as a csv:

Year,Name,Nationality,Club Team
 2000,Luis Figo,Portugal,Real Madrid
 2001,Michael Owen,England,Liverpool
 2002,Ronaldo,Brazil,Real Madrid
 2003,Pavel Nedved,Czech Republic,Juventus
 2004,Andriy Shevchenko,Ukraine,Milan
 2005,Ronaldinho,Brazil,Barcelona
 2006,Fabio Cannavaro,Italy,Real Madrid
 2007,Kaka,Brazil,Milan

Now let’s read it!

Creating a Reader Object

When we open the file, Python’s open() function will return a file object. In order to read this file object, however, we will need to create a csv.reader object. Python’s built-in library makes this easy:

import csv

with open('mvp_winners.csv', 'r') as file:
    mvp_winners_reader = csv.reader(file, delimiter=',')

The “delimiter” is a parameter that tells the csv reader what to look for as the separator of each value. Because we are working with a csv, this should be a comma.

NOTE: The csv reader has lots of functionality that can be used to manipulate the file in all sorts of ways. We will only cover a couple here, but if you are interested in more, check out the Python Documentation.

Reading the File

The csv reader allows us to index the values. For each row, row[0] is the year, row[1] is the name, and so on. Using this indexing, we can write a script to output some information. Say we want to know the winner of the award in 2004:

import csv

with open('mvp_winners.csv', 'r') as file:
    mvp_winners_reader = csv.reader(file, delimiter=',')
    
    target_year = "2004"
    for row in mvp_winners_reader:
         if row[0] == target_year:
              print(f'The winner in {row[0]} was {row[1]}, from {row[2]}')

This script iterates through each row until it finds the year we are looking for. It will return:

The winner in 2004 was  Andriy Shevchenko, from  Ukraine

Step 3: Writing to the File

Instead of reading the file, let’s say we want to add to it. There are a few things to keep in mind as we try to do this.

(Over)writing the file

We can easily write a new .csv file using “write” mode and creating a writer object:

import csv

with open('new_file.csv', 'w') as file:
    file_writer = csv.writer(file, delimiter=',')

However, for our current csv file, this will not work. You must be very careful when using the “write” mode in Python, because it will automatically overwrite anything that is already in the file. We’ll need to find a different solution.

NOTE: the “write” mode is only for files that already exist on your computer. If you want Python to create a new file, use “x” in place of “w.”

Append Mode

Instead of writing a new file, we can add to it using “append” mode. We simply replace the “w” with an “a”:

import csv

with open('mvp_winners.csv', 'a') as file:
    mvp_winners_writer = csv.writer(file, delimiter=',')

Once we are in append mode, we can use the already-built-in indices to easily create a new entry:

import csv

with open('mvp_winners.csv', 'a') as file:
    mvp_winners_writer = csv.writer(file, delimiter=',')
    
    mvp_winners_writer.writerow(["2008", "Cristiano Ronaldo", "Portugal", "Manchester United"])

And sure enough, it appears in our file:

...
2005,Ronaldinho,Brazil,Barcelona
2006,Fabio Cannavaro,Italy,Real Madrid
2007,Kaka,Brazil,Milan
2008,Cristiano Ronaldo,Portugal,Manchester United

Step 4: Putting it All Together

Let’s say we want to make a new .csv file, with only Brazilian winners of the award. We can do so using the tools that we have learned:

import csv

with open('mvp_winners.csv', 'r') as read_file, open('brazilian_winners.csv', 'x') as write_file:
	mvp_winners_reader = csv.reader(read_file, delimiter=',')
	brazilian_winners_writer = csv.writer(write_file, delimiter=',')
	
	for row in mvp_winners_reader:
		if row[2] == "Brazil":
			brazilian_winners_writer.writerow(row)

Here’s our result:

Year,Name,Nationality,Club Team
2002,Ronaldo,Brazil,Real Madrid
2005,Ronaldinho,Brazil,Barcelona
2007,Kaka,Brazil,Milan

Conclusion

This tutorial has outlined the very basics of reading and writing .csv files using Python’s built-in libraries. Hopefully, it will give you a good idea of the syntax and functionality, so that you can tailor the functions to your own needs! I encourage you to check out the Documentation for the csv library, as well as pandas, which allows for more advanced manipulation of csv files. Thank you for reading!

David

5 Comments

  1. This was a very useful tutorial! I learned Python in intro CS, but it’s been a little while and this was super helpful for recalling some of the things I had forgotten.

  2. Personally I liked this tutorial, because I’m an intended CS major. I also liked the way you give your step by step instructions by including the results of the inputted code. However I think that this tutorial may be a bit difficult for people who are not in CS to follow, because this language may seem a bit overwhelming or intimidating.

  3. I had no idea this was a thing python could do! Pretty easy to follow too, given I was able to just copy and paste the code into my own python file (+ grab the csv from the version you gave).

  4. Just like Henrie, this reminded me of my days in Intro to CS, learning how to read files and using the data collected in some way. Your tutorial made me wonder how different (or very similar) the code would look like Java.

  5. Great tutorial, David! I’m so glad that I came across this since I’ve just started working on a project in Python for my econ class!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.