Storing data in files lets you keep a record of the data with which a program is working. This means you don’t have to generate data over again when working with a program. You just read that data from a file.
To read files, use the readlines()
method. Once you’ve read a file, you use split()
to turn those lines into a list.
In this guide, we discuss how to use the split()
method to read a text file into a list. We’ll refer to an example so you can get started reading text files into lists quickly.
Python: Read Text File into List
Let’s start with a text file called grilled_cheese.txt. This file contains the ingredients for a grilled cheese sandwich. Our file content looks like this:
2 tbsp, ricotta 1 tbsp, grated parmesan 50g, mozzarella 25g, gorgonzola 2, thick slices white bread 1 tbsp, butter
The first column in our file contains the quantity of each ingredient to be used. The second column contains the name of an ingredient.
We read this file into our code using the open()
and readlines()
methods:
with open("grilled_cheese.txt", "r") as grilled_cheese: lines = grilled_cheese.readlines() print(lines)
In our code, we open a file called “grilled_cheese.txt” in read mode. Read mode is denoted by the “r” character in our open()
statement. Next, we print those lines to the console.
Let’s see what our Python code returns:
['2 tbsp, ricotta\n', '1 tbsp, grated parmesan\n', '50g, mozzarella\n', '25g, gorgonzola\n', '2, thick slices white bread\n', '1 tbsp, butter\n']
Our code returns a list of each line in our file. This is not quite the output we are expecting. While we’ve read our file into a list, we have a problem: each line is stored in its own string. Ingredients and their quantities are not separate.
Divide Values into a List
To solve this problem, we use the split() method. This method lets us divide a string using a separator character we specify.
To start, we declare two lists: quantities and ingredients. This code will remain indented because it is part of our open()
block of code.
quantities = [] ingredients = []
We’ll iterate over our list so we can access each line of text from our file. Then we’ll split each line into two parts. The dividing point is the comma followed by a space on each line:
for l in lines: as_list = l.split(", ") quantities.append(as_list[0]) ingredients.append(as_list[1])
The for loop lets us read our file line by line. The first value in “as_list” is the quantity of an ingredient. The second value is the name of the ingredient. We then print both of these lists to the console:
print(quantities) print(ingredients)
Let’s run our code:
['2 tbsp, ricotta\n', '1 tbsp, grated parmesan\n', '50g, mozzarella\n', '25g, gorgonzola\n', '2, thick slices white bread\n', '1 tbsp, butter\n'] ['2 tbsp', '1 tbsp', '50g', '25g', '2', '1 tbsp'] ['ricotta\n', 'grated parmesan\n', 'mozzarella\n', 'gorgonzola\n', 'thick slices white bread\n', 'butter\n']
Our code prints three lists to the console. The first list is a list of all the lines of text in our file. The second list contains all the quantities from our file. The third list contains all the ingredients.
Remove New Lines
There is still one improvement that we need to make. Every ingredient ends in the “\n” character. This character denotes a new line. We can remove this character by using the replace() method:
for l in lines: as_list = l.split(", ") quantities.append(as_list[0]) ingredients.append(as_list[1].replace("\n", ""))
In our for loop, we replace the value “\n” with an empty string. We do this on the as_list[1] value which correlates to the name of each ingredient.
Now that we’ve made this change, our program is ready:
with open("grilled_cheese.txt", "r") as grilled_cheese: lines = grilled_cheese.readlines() quantities = [] ingredients = [] for l in lines: as_list = l.split(", ") quantities.append(as_list[0]) ingredients.append(as_list[1].replace("\n", "")) print(quantities) print(ingredients)
Let’s run our code and see what happens:
['2 tbsp', '1 tbsp', '50g', '25g', '2', '1 tbsp'] ['ricotta', 'grated parmesan', 'mozzarella', 'gorgonzola', 'thick slices white bread', 'butter']
Our code successfully transforms our text file into two lists. One list contains the quantities of ingredients for a recipe. The other list contains the ingredients we’ll use for the recipe.
Conclusion
You can read a text file using the open()
and readlines()
methods. To read a text file into a list, use the split()
method. This method splits strings into a list at a certain character.
In the example above, we split a string into a list based on the position of a comma and a space (“, ”). Now you’re ready to read a text file into a list in Python like an expert.
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.