In this blogpost we show you how to split a textfile by lines in your Python program. Suppose you have a text file containing months and seasons, like so:
Let us name this file “seasons.txt”. Now we show you how to read this text file and split it into lines inside your Python program for ease of processing.
Method 1: Use a for loop to read each line
The easiest way is to open the file using the open() function and read all its contents into a variable. Then you use a for loop to cycle through this variable and print each line, one at a time.
Consider the program:
This produces the output:
Note that there is an extra newline after each line. This is because, in addition to the newline in the file after each line, the print() function adds its own newline after each invocation. Within the for loop you can do any special processing you desire on the variable.
For instance, you can remove the newlines with the following program:
The output is:
Method 2: Use a list comprehension
A second way to split a text file line by line is using list comprehension syntax. Consider the program:
This will output:
If you desire pretty printing, you can use another loop, like so:
This will output as before:
Method 3: Use split() or splitlines()
The split() method helps split a string into lists of fields. Thus the third method for splitting our text file looks like this:
We read each line as before into the variable “x” and then use split to convert each line into a list. The output is:
The split() method takes an argument which is the (typically) whitespace character that is used to split each line. If instead of whitespace the fields are separated by a pipe (“|”) character then this should be given as an argument to the split() method.
The splitlines() method behaves for the most part like the split() method except in edge cases involving empty strings and terminal line breaks. Its arguments are also different. Thus the code:
Method 4: Use a generator
The final method we will encounter is considered a “lazy” method and is suitable for reading very large files. It splits a text file line by line but doesn’t do it all in one stroke. Instead it provides lines one by one on demand.
First we write a function called “lazy_read”:
Note that in this function we use the “yield” statement instead of return. The yield statement suspends the function’s execution (in this case, after returning the first line read) but when it is invoked again it remembers enough state to continue with the execution (to the second line, and so on). In this manner, you do not get overwhelmed with large files and can read them line by line.
Now here is how we use this function:
The output is as before:
You have learnt four different ways to read a text file and split it into lines in Python. Which one is your favorite?
Kodeclik is an online coding academy for kids and teens to learn real world programming. Kids are introduced to coding in a fun and exciting way and are challeged to higher levels with engaging, high quality content.