0

I need to get a range of values from a large text file that contains epoch time stamps. I am currently looping through the file for start/end points, retrieving the line numbers, then iterating over the list again to retrieve the range values. This feels very inefficient because it involves looping over the list twice. Is there a better way?

start = '1509347040'
end = '1509347700'
with open('database.txt') as fp:
    for i, line in enumerate(fp):
        if start in line:
            start=i
        elif end in line:
            end=i
            break
    for i, line in enumerate(fp):
        if i >= start and i <= end:
            print(i,line)
Skyros
  • 37
  • 1
  • 6

1 Answers1

2

You can use a flag variable to keep track of the moment you encountered start. Something like this. This way you will traverse through the file only till the end (in the worst case, only once through the file).

flag = 0
with open('database.txt') as fp:
    for i, line in enumerate(fp):
        if start in line:
            flag = 1
        elif end in line:
            print(i,line)
            break
        if flag==1:
            print(i,line)
Miraj50
  • 4,257
  • 1
  • 21
  • 34