Medya Gh
Medya Gh

Reputation: 5023

python seek reading file to a specific line

I would like to read a very large file from a line which has a sepecfic word, what is the best way to do that ?

lets say it is a file with 50K lines

43511
24622
53213
43534
57656
12121

I want to start reading lines of this file from the line that has 43534, what would be the most efficient way for a large file?

Upvotes: 0

Views: 4229

Answers (3)

iruvar
iruvar

Reputation: 23364

You could use itertools.dropwhile

t = '''43511
24622
53213
43534
57656
12121
'''


from StringIO import StringIO
import os
from itertools import dropwhile
from contextlib import closing

with closing(StringIO(t)) as f:
    for x in dropwhile(lambda x: x != '43534' + os.linesep, f):
            print x

Upvotes: 3

Paulo Bu
Paulo Bu

Reputation: 29794

One way to do it manually without heavily exploding the memory could be something like this:

f = open('file.txt','r')
found = False
for line in f
    if line == '43534':
        found = True
    if found:
        # you now reached the line in the file and
        # therefore you can begin process it here
        # in case you need the position of the buffer
        # you do: f.tell()

Hope this helps!

Upvotes: 1

David Marx
David Marx

Reputation: 8558

Just create a binary variable to represent whether or not you've read in that particular target string you are looking for. When you reach the string, flip the flag, triggering your script to read the rest of the file.

test = '43534'
past_test = False
with open(fname,'r') as f:
    for line in f:
        if past_test:
            # do stuff                
        elif line == test:
            past_test = True

Upvotes: 1

Related Questions