Read some specific lines from a big file in python

by fahimnawaz7   Last Updated October 20, 2019 05:26 AM - source

I want to read some specific lines from a large text file where line numbers are in a list, for example: list_Of_line =[3991, 3992, ...]. I want to check whether there is the string "this city" in line number 3991, 3992,... or not. I want to directly access those lines. How can I do it in python?

Answers 1

There is no way to "directly access" a specific number of line of a file outright, since lines can start at any position and can be of any lengths. The only way to know where each line is in a file is therefore by reading every character of the file to locate each newline character.

Understanding that, you can iterate through a given file object to build a list of file positions of the end of each line by calling the tell method of the file object, so that you can then "directly access" any line number you want with the seek method of the file object to read the specific line:

list_of_lines = [3991, 3992]
with open('file.txt') as file:
    positions = [0, *(file.tell() for line in file)]
    for line_number in list_of_lines:[line_number - 1])
        if 'this city' in next(file):
            print(f"'this city' found in line #{line_number}")
October 20, 2019 05:42 AM

Related Questions

How to write comments in batch files for ffmpeg?

Updated October 09, 2019 22:26 PM

How to switch the order of text in a line?

Updated November 19, 2017 22:26 PM

How to read specific lines from a text file in PHP

Updated February 10, 2018 20:26 PM