How to Split a File into a List in Python
Last Updated :
22 Jun, 2022
In this article, we are going to see how to Split a File into a List in Python.
When we want each line of the file to be listed at consecutive positions where each line becomes an element in the file, the splitlines() or rstrip() method is used to split a file into a list. Let's see a few examples to see how it's done.
The file is opened using the open() method where the first argument is the file path and the second argument is a string(mode) which can be 'r' ,'w' etc.. which specifies if data is to be read from the file or written into the file. Here as we're reading the file mode is 'r'. the read() method reads the data from the file which is stored in the variable file_data. splitlines() method splits the data into lines and returns a list object. After printing out the list, the file is closed using the close() method.
Create a text file with the name "examplefile.txt" as shown in the below image which is used as an input.
Python3
# opening the file
file_obj = open("examplefile.txt", "r")
# reading the data from the file
file_data = file_obj.read()
# splitting the file data into lines
lines = file_data.splitlines()
print(lines)
file_obj.close()
Output:
['This is line 1,', 'This is line 2,', 'This is line 3,']
Example 2: Using the rstrip()
In this example instead of using the splitlines() method rstrip() method is used. rstrip() method removes trailing characters. the trailing character given in this example is '\n' which is the newline. for loop and strip() methods are used to split the file into a list of lines. The file is closed at the end.
Python3
# opening the file
file_obj = open("examplefile.txt", "r")
# splitting the file data into lines
lines = [[x.rstrip('\n')] for x in file_obj]
print(lines)
file_obj.close()
Output:
[['This is line 1,'], ['This is line 2,'], ['This is line 3,']]
Example 3: Using split()
We can use a for loop to iterate through the contents of the data file after opening it with Python's 'with' statement. After reading the data, the split() method is used to split the text into words. The split() method by default separates text using whitespace.
Python3
with open("examplefile.txt", 'r') as file_data:
for line in file_data:
data = line.split()
print(data)
Output:
['This', 'is', 'line', '1,']
['This', 'is', 'line', '2,']
['This', 'is', 'line', '3,']
Example 4: Splitting a text file with a generator
A generator in Python is a special trick that can be used to generate an array. A generator, like a function, returns an array one item at a time. The yield keyword is used by generators. When Python encounters a yield statement, it saves the function's state until the generator is called again later. The yield keyword guarantees that the state of our while loop is saved between iterations. When dealing with large files, this can be useful
Python3
# creating a generator function
def generator_data(name):
# opening file
file = open(name, 'r')
while True:
line = file.readline()
if not line:
# closing file
file.close()
break
# yield line
yield line
data = generator_data("examplefile.txt")
for line in data:
print(line.split())
Output:
['This', 'is', 'line', '1,']
['This', 'is', 'line', '2,']
['This', 'is', 'line', '3,']
Example 5: Using list comprehension
Python list comprehension is a beautiful way to work with lists. list comprehensions are powerful and they have shorter syntax. Furthermore, list comprehension statements are generally easier to read.
To read the text files in previous examples, we had to use a for loop. Using list comprehension, we can replace ours for loop with a single line of code.
After obtaining the data through list comprehension, the split() is used to separate the lines and append them to a new list. let's see an example to understand.
Python3
with open("examplefile.txt", 'r') as file:
data = [line.strip() for line in file]
print(data)
# iterating through data
for line in data:
print(line.split())
Output:
['This is line 1,', 'This is line 2,', 'This is line 3,']
['This', 'is', 'line', '1,']
['This', 'is', 'line', '2,']
['This', 'is', 'line', '3,']
Example 6: Splitting a single text file into multiple text files
If we have a large file and viewing all the data in a single file is difficult, we can split the data into multiple files. let's see an example where we split file data into two files.
Python list slicing can be used to split a list. To begin, we read the file with the readlines() method. The file's first-half/upper half is then copied to a new file called first half.txt. Within this for loop, we'll use list slicing to write the first half of the main file to a new file.
A second loop is used to write the other part of the data into a second file. The second half of the data is contained in the second half.txt. To perform the slice, we need to use the len() method to determine the count of lines in the main file Finally, the int() method is used to convert the division result to an integer value
Python3
# opening the main file
with open("examplefile.txt", 'r') as file:
data = file.readlines()
# writing half of the data in one file
with open("first_half.txt", 'w') as file1:
for line in data[:int(len(data)/2)]:
file1.write(line)
# writing another half of the data in one file
with open("second_half.txt", 'w') as file2:
for line in data[int(len(data)/2):]:
file2.write(line)
Output:
Similar Reads
Python Tutorial | Learn Python Programming Language Python Tutorial â Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly.Python is:A high-level language, used in web development, data science, automatio
10 min read
Python Interview Questions and Answers Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth
15+ min read
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Python OOPs Concepts Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p
11 min read
Python Projects - Beginner to Advanced Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list
10 min read
Python Exercise with Practice Questions and Solutions Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test
9 min read
Python Programs Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co
11 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Enumerate() in Python enumerate() function adds a counter to each item in a list or other iterable. It turns the iterable into something we can loop through, where each item comes with its number (starting from 0 by default). We can also turn it into a list of (number, item) pairs using list().Let's look at a simple exam
3 min read