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,']

How to Split a File into a List in Python

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.

Similar Reads

Example 1: Using the splitlines()

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....

Example 2: Using the rstrip()

...

Example 3: Using split()

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....

Example 4: Splitting a text file with a generator

...

Example 5: Using list comprehension

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....

Example 6: Splitting a single text file into multiple text files

...

Contact Us