简体   繁体   中英

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?

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

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM