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
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.
The code so far now looks like this:
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.
The code so far now looks like this:
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:
The code so far now looks like this:
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.
The code so far now looks like this:
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.
The final code looks like this:
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.
2. When a match has been found (inside the IF statement) add a line of code to change that variable to:
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.
4. Finally code what you want to happen if there are no matches inside the if
.
The final code now looks like this:
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:
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:
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:
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 thecheckanother
variable is equal toY
. 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: