i2tutorials

How to read a file line-by-line into a list in Python?

 

Read a file line-by-line into a list

These are the prerequisites required before we jump into the solution:

 

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.

 

Using readlines()

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:

 

Example:

Telangana : Hyderabad
Andhra Pradesh : Amaravathi
Tamil Nadu : Chennai
Kerela : Trivendrum
Karnataka : Bangalore

Output:

 

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.

 

Example:

with open("states_capitals.txt", "r") as states_capitals:
   lines = states_capitals.readlines()
   print(lines)

Output:

 

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.

split() method

 

 First, we declare two lists: states and capitals. 

 

Example:

[ ]
      states = []
      capitals = []

Output:

 

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.

 

Example:

[31]   for l in lines:
          as_list = l.split(", ")
          states.append(as_list[0])
          capitals.append(as_list[0].replace("\n", ""))

Output:

 

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.

 

Example:

print(states)
print(capitals)

Output:


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.

 

Example:

with open("states_capitals.txt", "r") as states_capitals:
   lines = states_capitals.readlines()

   states = []
   capitals = []

   for l in lines:
      as_list = l.split(", ")
      states.append(as_list[0])
      capitals.append(as_list[0].replace("\n", ""))

   print(states)
   print(capitals)

Output:

 

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.

 

Exit mobile version