How To Efficiently Read Set Lines of Range in Text File


In order to crawl a large text file containing URLs, I required a method that would allow me to specify and read only a certain range of lines, without having to load the entire file into memory. To achieve this, I utilized the islice() function from Python's itertools module: itertools.islice()

Here is a code snippet that demonstrates this:

itertools.islice() function efficiently reads a range of lines from a large text file in Python by using the lazy evaluation technique. It doesn’t read the entire file at once, but instead reads only the lines that are required for the specified range.

When you use islice() to read a range of lines from a file, it returns an iterator that produces those lines. This iterator can be used in a loop or converted to a list or any other iterable. Since the islice() function returns an iterator, it doesn’t consume the memory and can efficiently handle large text files.

from itertools import islice

def read_file(filepath, start, end):
    with open(filepath) as input_file:
        lines = islice(input_file, start - 1, end)        
        lines = map(lambda s: s.strip(), lines)       
        for line in lines:            

read_file('bigfile.txt', 1, 430)
read_file('bigfile.txt', 4000, 6500)

You might also like: How to Detect Backspace Event in UITextfield and UITextview?

More Articles

Recommended posts