Skip to content » Python Unit Home » Python CSV Files Tutorial

Python CSV Files Tutorial

CSV → 2D List

Reading a CSV File into a 2 Dimensional Python List

Sometimes you will need to read data stored in comma separated value (CSV) files. You can use the csv module in order to achieve this.

import csv

f = open('mycsvfile.csv','r')
reader = csv.reader(f, delimiter = ',')

items = []
for row in reader:

CSV files usually use a comma as the delimiter between each data item, but sometimes might use a tab or other character instead.

Tutorial Video

YouTube Blocked? Try the Google Drive Version

2D List → CSV

Writing a 2 Dimensional List to a CSV File

Often you will want to be able to save 2 Dimensional List data to a CSV file so that it isn’t deleted when your program exits. you can use csv writer to achieve this.

import csv
f = open('animals.csv','w', newline = '')
#use 'a' instead of 'w' if you want to add to the csv, not write a new one.
writer = csv.writer(f)
# example 2D List
l = [["dog",2],["cat",15],["mouse",34]]

for item in l:

f.close() #really important!!!

Tutorial Video

YouTube Blocked? Use the Google Drive version instead

CSV → Dictionary

Reading a CSV File into a Python Dictionary

Sometimes you want to read the data into a dictionary instead of a 2 Dimensional list. CSV reader can do this too.

import csv

f = open('people.csv','r')
reader = csv.reader(f, delimiter = ',')

people = {}
for row in reader:
     people[row[0]] = {'age':row[1],'color':row[2]}

Tutorial Video

Can’t access YouTube? Try the Google Drive Version.

Dictionary → CSV

Writing a Python Dictionary to a CSV File

import csv

cols = ["name","strength","speed"]

players = [

f = open("players.csv", "w", newline='')
writer = csv.DictWriter(f, fieldnames = cols)
writer.writeheader() #If you want a header row in your csv

Challenge 37

Challenge 37 – Animal Stats Printer


  • Read the CSV file animal_data.csv into a 2 dimensional list and print out the items.


  • Print out the list in order of population


  • Ask the user to type in a column selection(e.g. “average age”) and have the program sort the items in that order.


Challenge 38 – Animal Stats Adder


  • Write a program that allows you to add an extra animal to the animal_facts.csv file. (you might want to use append ‘a’ mode instead of  write ‘w’ mode.)


  • Allow the user to keep adding more animals until the type an empty string.


  • Once the user has finished adding, print out the animals in alphabetical order.


Challenge 39 – IT Support Log


  Ask the user to input their name and a description of their IT issue. Save the issue to the file ‘ict_issue_log.txt’


 Adapt your program so that it automatically adds the current date and time to the log.


 Adapt your program so that each ticket is given a unique job number that increments automatically.


Challenge 40 – Country Top Trumps


Create program that reads the country_csv file into a list of dictionaries.


Create a one player top trumps game where the player picks a single card and the computer picks a card and then they see who wins.


Extend the game so that you play a full game of top trumps with a deck of card and two players