Skip main navigation

Using CSV in Python

Using the CSV library to read and write to a file in Python.

Comma-separated value files offer simple persistent structured storage. You can work with CSV files using standard Python file methods, but using the Python CSV library makes it easier to handle data from a CSV file.

In the previous step you created a CSV file called foods.csv which contained the names and favourite foods of a group of people:

 Name,Food
Sarah,Cheese
Dexter,Chicken
Li,Burgers
Amy,Ice cream

Last week you learnt how to read from and write to files, and this same principle can be used when working with CSV files.

Reading the contents of a CSV file

You should recognise the code below from last week. It reads the data from the file and then displays all of the entries in the CSV file in the REPL.

 with open("foods.csv","r") as f:
data = f.read()
print(data)

Writing to a CSV file

A CSV file can be written to or appended using the open() method just like any other file:

 name = "Les"
food = "Lasagne"
f = open("foods.csv","a")
f.write(name + "," + food + "n")
f.close()

This example appends a new line to the CSV file which adds a new name and their favourite food to the CSV file. It uses two variables, name and food, to reference the person and their food choice. Why do you think you might do that rather than writing in "Les, Lasagnen"?

Reading and writing data to a file using the standard file methods in Python means that the data is read as a string. This means that you may need to use string slicing to select the data that you want. It is often easier to work with CSV files using the csv library.

Reminder: String slicing allows you to take a ‘slice’ of a longer string, e.g. removing www. from the start of the URL www.google.com to give google.com.

Using the CSV library to read a file

Python has a special library installed as standard which can work with CSV files. Using this library you can write code to read from and write to CSV files with far greater flexibility than using standard file handling methods.

 import csv
with open('foods.csv') as csvfile:
favourites = csv.reader(csvfile, delimiter=',')
for row in favourites:
print(row)

This code first imports the Python CSV library. It then opens foods.csv and creates a reference to the file as csvfile. The code then reads the file using the csv.reader() method and stores the data to an object called favourites. The data in favourites is stored so that it can be iterated over row by row with each row (a line from the original file) being output as a list. The code does this and prints each row to the REPL as a list.

Tip: An object is a collection of data, and functions that can act on that data.

Using the CSV library to write to a file

You can add another row of favourite foods to the CSV file using the same CSV library. Take a look at the code below. Can you work out what each line is doing?

 import csv
name = "Portia"
food = "Steak"
with open('foods.csv', mode="a") as csvfile:
favourites = csv.writer(csvfile, delimiter=',')
favourites.writerow([name,food])

After importing the library this code will open the file in a similar manner to before, but with the mode set to "a" for append. This will enable the code to append new data to the file foods.csv. This matches the syntax used in the open() method.

The code then uses the csv.writer() method to prepare to write to the file, with the delimiter ',' being used to separate each data item being written.

All of this configuration will be saved to an object called favourites, which is then used with the favourites.writerow() method to append a new row at the bottom of the file. This row is made from the contents of a list, which in this case contains the variables name and food.

Why is the CSV library so important?

You could just use the standard file handling methods with the CSV file, but using the CSV library, which was specifically created for working with CSV data, you have more functionality. For example, you can use csv.reader to read any data not in " " as a float type by using the quoting=csv.QUOTE_NONNUMERIC argument.

 import csv
with open('numbers.csv', newline= '') as csvfile:
favourites = csv.reader(csvfile, delimiter=',', quoting=csv.QUOTE_NONNUMERIC)
print(type(favourites))
for row in favourites:
print(type(row[0]), type(row[1]))

Here the code reads the contents of the numbers.csv file, which contains a series of strings and numerical values. The non-string data is converted into floats (real numbers) automatically.

All of this is handled by the library. If you were to use the open() method then you would require additional lines of code to cast the strings into floats. Also, remember that the row data returned using csv.reader() is returned as a list, which means that you can easily pick out individual pieces of data, unlike using the open() method which would require string slicing to extract the data.

This article is from the free online

Programming 103: Saving and Structuring Data

Created by
FutureLearn - Learning For Life

Reach your personal and professional goals

Unlock access to hundreds of expert online courses and degrees from top universities and educators to gain accredited qualifications and professional CV-building certificates.

Join over 18 million learners to launch, switch or build upon your career, all at your own pace, across a wide range of topic areas.

Start Learning now