How to read a file line-by-line into a list?
These are the prerequisites required before we jump into the solution:
- Access modes
- Open a file
- Close a file
Python is enabled with inbuilt functions which allow us to create, write, and read files. We can handle normal text files and binary files (written in binary language, 0s, and 1s) in Python. Now let’s learn how to read a file line by line.
readlines() is one of the three ways to read data from any text file. It returns the data i.e. each line as a string element in a list. This function can be used for small files, as it reads the whole file content to the memory, then splits it into separate lines. We can iterate over the list and strip the newline ‘\n’ character using strip() function.
Let’s consider a text file called state_capital.txt. This file contains data of different states and their capitals. Our file content looks like this:
We now try to open this file using open() in read mode. We put an “r” character in the open() statement next to the file name.
The code returns a list of each line in our file. But, this is not what we were expecting as the output. We were able to read our file into a list, but each line is getting stored as a string.
So the solution to this would be using the split() method. This method allows us to divide a string using a specified separator character.
First, we declare two lists: states and capitals.
We’ll now iterate over our list and then split each line into two parts where the separator is a comma with a space next to it on each line.
It’s the for loop which lets us read our text file line by line. The first value in “as_list” is the state. The second value is its respective capital.
This prints three lists: A list of all the lines of text in our file, a list containing all the states and finally a list containing all the capitals.
This is our final code which helps us to read a file line-by-line into a list
Note: Make sure that the text file and the python program file are present in the same directory, else we need to specify the entire path to the text file in the place of the filename.