Sometimes you will need to read data from a file to find information and then depending on what you find do something with it.

The process of reading from a file is always the same. The only thing that changes is what file you are using and what you are looking for in that file.

Below are the key steps to the process:

  • Open a connection to a file
  • Take input from the user to find out what you are looking for
  • Read through each line of the file
  • When you read a line split the information into a list (so you can check individual pieces of information)
  • Check if the piece of information entered by the user is in the line
  • If it is do whatever you need to do.

CSV Files

When reading data from a file, the easiest file type to use is a CSV file, this stands for comma separated values.

Consider the file below that contains a list of games for different consoles:

Each piece of data is separated by a comma. This means that when you split the information up you can say to the program split the information into the list every time you find a comma.

This means you can then refer to elements of the list to extract single pieces of information. Imagine the list you have is called games, the second image above shows how you would reference the information. If you wanted the name of the game it would be games[1], if you wanted to the rating it would be games[3]

Worked example - File Reading - Speeding Cars

Before creating a program to read data from a file you need to understand what data is in the file. In this example it will use a file called cars.csv, which is shown below:

In this file there are two pieces of data, these are the car registration and the speed. When the data is split into a list, they can be referred to as element 0 for the registration and element 1 for the speed.

Step 1 - Saving the CSV file

You must always make sure the CSV file is saved in the same location as the Python file.

Step 2 - Creating a Connection to the file

file = open("cars.csv", "r")

This code will create a connection to the file cars.csv and r mode which stands for read. It can then be referred to as the variable name which is file

Step 3 - Taking Input from the User 

In this program we need to find out the speed limit so we can compare it to the speed that the car is travelling to see if it is speeding, therefore we need to ask the user the speed limit.

limit = int(input("Please enter the speed limit: "))

The code so far now looks like this:

file = open("cars.csv", "r")
limit = int(input("Please enter the speed limit: "))

Step 4 - Reading through the file 

Now each line of the file needs to be checked, you will need to use a loop to do this.

for line in file:

The code so far now looks like this:

file = open("cars.csv", "r")
limit = int(input("Please enter the speed limit: "))
for line in file:

Step 5 - Read a line from the file and split the data up 

Now we have a loop that will read each line in the file, we need to read one line at a time, and split the information up. We know it is a csv file and therefore each piece of data is separated by a comma. When it splits the line from the file it needs to store it in a list, the list below is called details. As the piece of information we need from the file is the speed, the program then takes element 1 from the list and stores it as speed:

details = line.split(",")
speed = float(details[1])

The code so far now looks like this:

file = open("cars.csv", "r")
limit = int(input("Please enter the speed limit: "))
for line in file:
    details = line.split(",")
    speed = float(details[1])

Step 6 - Checking the Data 

The next stage is to check the data that we have read from the file. In this case we want to see if the speed is greater than (>) the speed limit.

if speed > limit:

The code so far now looks like this:

file = open("cars.csv", "r")
limit = int(input("Please enter the speed limit: "))
for line in file:
    details = line.split(",")
    speed = float(details[1])
    if speed > limit:

Step 7 - If the condition is met 

If the condition is met and in this case the car is speeding then you can type the code you want to run like you would for a normal if statement. In this example we want to display the information of the speeding cars.

print(details[0] + " " + details[1])

The final code looks like this:

file = open("cars.csv", "r")
limit = int(input("Please enter the speed limit: "))
for line in file:
    details = line.split(",")
    speed = float(details[1])
    if speed > limit:
        print(details[0] + " " + details[1])

The finished program looks like this:

Dealing with no matches found in the file

Sometimes the information you are looking for in a file might not be there, you will need to let the user know this.

At the moment when you enter a speed limit where there are no cars speeding, the program does this:

What it should do is display a message something like There are no speeding cars, as shown below:

How to do this:

1. Create a boolean variable at the start of the program called found and set it to False. This is because at the start of the program no matches have been found.

found = False

2. When a match has been found (inside the IF statement) add a line of code to change that variable to:

if speed > limit:
    #new line of code
    found = True 
    print(details[0] + " " + details[1])

3. After the for loop create an if that checks if found == False. If found is still False and it has been through the loop and read the whole file and it is still False this means that the information you have been looking for in the file has not been found.

if found == False:

4. Finally code what you want to happen if there are no matches inside the if.

if found == False:
    print("There are no speeding cars")

The final code now looks like this:

# new code
found = False 
file = open("cars.csv", "r")
limit = int(input("Please enter the speed limit: "))
for line in file:
    details = line.split(",")
    speed = float(details[1])
    if speed > limit:
        # new code
        found = True 
	print(details[0] + " " + details[1])
if found == False: #new code
    # new code
    print("There are no speeding cars") 

NOTE: You must use a variable to do this rather than adding an else to the if as otherwise it could display the message multiple times, for example:

Example Program 1 - Timetable Program

This program asks the user to enter the day you want to view the timetable for. It will then look in the file and find the lessons for that day or say that it cannot be found.

The CSV file looks like this:

file = open("timetable.csv", "r") #opens the file
day = input("Enter the day you want to view your timetable for: ") #gets the input from the user
found = False #stores whether the day is found in the file, set to False at the beginning
for line in file: #reads each line in the file
    timetable = line.split(",") #splits the line into the timetable list
    if day == timetable[0]: #checks if the day entered is in element 0 of the list
	#if it is it prints the timetable referring to the different elements of the list
        print("On " + timetable[0] + " your lessons are: \n"+
              "Period 1 - " + timetable[1] + "\n Period 2 - " +
              timetable[2] + "\n Period 3 - " + timetable[3] +
              "\n Period 4 - " + timetable[4] + "\n Period 5 - " + timetable[5])
	#sets found to True as a match is found
        found = True
if found == False: #after the loops checks if found if still False
    #displays a message if it is
    print("Please enter a day between Monday and Friday")

When run in IDLE:

Example Program 2 - Stock Checker

This program asks the user to enter the games console they are looking for. It will then look through the file and calculate the total value of each game in stock (the number there are multiplied by the price). If there are no games for that console it will say Console not found.

The CSV file looks like this:

console = input("Please enter a console (Xbox1, Xbox, PS3, PS4): ") #gets the input from the user
file = open("games.csv", "r") #opens the file
found = False #stores whether the console is found in the file, set to False at the beginning
gamevalue = 0 #stores the total value of a game
for line in file: #reads each line in the file
    details = line.split(",") #splits the line into the details list
    if details[0] == console: #checks if the console entered is in element 0 of the list
        found = True #sets found to True as a match is found
	#variables that store different pieces of information from the list to make them easier to reference
        game = details[1]
        price = float(details[2])
        rating = details[3]
        noinstock = int(details[4])
        gamevalue = price * noinstock #calculates the total stock value of a game
	#prints the game information to screen
        print(game + " " + str(price) + " " + rating + " " + str(noinstock) + " " + str(gamevalue))
if found == False: #after the loops checks if found if still False
    #displays a message if it is
    print("Console not found")

When run in IDLE:

Reading through a file multiple times

Sometimes you may need to read through a file to find something and if it doesn’t find what you are looking for then try to find something else. To do this you would need to create a loop to repeat the process.

Using the example of the Stock Checker if you enter the console PS4 it will say Console not found and then the program stops (see below). If you wanted to then try a different console you would have to run the program again.

If you inserted a loop into the program, it could ask you if you wanted to check another console and then repeat the process, like shown below:

This is how the code would change from the example above:

checkanother = "Y" #new line 1
while checkanother == "Y": #new line 2
    console = input("Please enter a console (Xbox1, Xbox, PS3, PS4): ")
    file = open("games.csv","r")
    found = False
    for line in file:
        details = line.split(",")
        if details[0] == console:
            game = details[1]
            price = float(details[2])
            rating = details[3]
            noinstock = int(details[4])
            gamevalue = price * noinstock
            print(game, str(price), rating, str(noinstock), str(gamevalue))
    if found == False:
        print("Console not found")
    checkanother = input("Check another console: Y/N ") #new line 3

How it Works

  • New Line 1 - this is a variable that keeps track of whether they want to check another console. This is set to Y at the start as when the program is ran for the first time they will want to check a console.
  • New Line 2 - this is a while loop that will repeat while the checkanother variable is equal to Y. All of the previous code is then put inside this loop.
  • New Line 3 - after the file has been checked once, the program asks whether the user wants to check another product, it will then go back to the start of the loop. If they have entered Y it will go around the loop again, if they have typed anything else it will not loop again and stop.

When run in IDLE: