Want to keep learning?

This content is taken from the Raspberry Pi Foundation & National Centre for Computing Education's online course, Programming 103: Saving and Structuring Data. Join the course to learn more.

Skip to 0 minutes and 3 secondsComma Separated Values, or CSV, files offer simple, persistent structural 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 the CSV file. Earlier this week, you created a CSV file called foods.csv, which contained the names and favourite foods of a group of people. Here is the CSV we file, and we can see some names and food.

Skip to 0 minutes and 40 secondsRemember this code. It reads the data from the file and displays all of the entries from the CSV file in the repo.

Skip to 0 minutes and 54 secondsA CSV file can be written to or appended using the open method just like any other file. In this example of appended a new line, which adds a new name and food to the CSV file, I've used two variables-- name and food-- to reference the person and their food choice. For example here, I have Eirini as the name and lasagna as the food, which I then append to the foods.csv file. 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.

Skip to 1 minute and 41 secondsIt is often easier to work with CSV files using a CSV library. This code first imports the Python CSV library. And it then opens foods.csv and creates a reference to the file as CSV file.

Skip to 2 minutes and 4 secondsThe code reads the file using the CSV reader method and stores the data to an object called favourites.

Skip to 2 minutes and 16 secondsThe data in favourites is stored so that it can be iterated over row by row with each row being output as a list. The code then prints every row accordingly. Try adding another row of favourite foods to the CSV a file using the same CSV library. Take a look at this code, and take a moment to consider what each line is doing.

Skip to 2 minutes and 51 secondsAfter importing the library, this code opens the file in a similar manner to before but with the mode set to a for append. The code appends new data to the file foods.csv matching the syntax used in the open method.

Skip to 3 minutes and 13 secondsThe code then uses the csv.writer method to prepare to write to the file with a delimiter comma 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 within the favourites.writerow method to append a new row at the bottom of the line. This row is made from the contents of a list, which in this case, contains the variable's name and food. Now try this code for yourself.

Using CSV 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, Lasagne\n"?

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.

Share this video:

This video is from the free online course:

Programming 103: Saving and Structuring Data

Raspberry Pi Foundation