0

I am playing around with python and I am trying to figure out a way to search a text file for a specific word within a time range. The file has time stamps, however since the file is a text file everything is a string.

Text file contains the below:

17:14:26.442 words words words words words

17:15:32.533 words words words words words

17:16:26.442 words words words words words

17:17:32.533 words words words words words

17:18:26.442 words words words words words

17:19:32.533 words words words words words

17:20:26.442 words words words words words

17:21:32.533 words words words words words

What I want to do is search for a word in a time frame and get back only that line that has word between 17:17:32.533 and 17:20:26.442. However, since its a text document and everything is a string I cannot use the range option. Does anyone have some suggestions on how I can do this?

user6534872
  • 63
  • 3
  • 9

2 Answers2

1

Use the datetime module to parse and convert the timestamp strings to datetime object and then you can use the comparison in order to check only the lines that fall in that time range.

from datetime import datetime as dt

start = dt.strptime('17:17:32.533','%H:%M:%S.%f')
end = dt.strptime('17:20:26.442','%H:%M:%S.%f')
word_to_search = 'word'
with open('sample.txt', 'r') as f:
    for line in f:
        ts=dt.strptime(line.split()[0],'%H:%M:%S.%f')
        if ts>start and ts<end:
            if word_to_search in line:
                print line
Wajahat
  • 1,593
  • 3
  • 20
  • 47
0

If the timestamps are exactly in the format you describe (HH:MM:SS.sss), then you can compare directly:

start = '17:17:32.533'
end = '17:20:26.442'
with open(filename, 'r') as f:
    for line in f:
        if line[:12] >= start and line[:12] <= end:
            print(line)

If this doesn't work, because e.g. 01:01:01.000 is outputted as 1:1:1.0, you have to parse the timestamp first. For example:

import datetime
start = datetime.time(17, 17, 32, 533)
end = datetime.time(17, 20, 26, 442)
with open(filename, 'r') as f:
    for line in f:
        timestamp, words = line.split(None, 1)
        time = datetime.strptime(timestamp, "%H:%M:%S.%f").time()
        if time >= start and time <= end:
            print(words)
Tim Fuchs
  • 1,171
  • 6
  • 15