Reputation: 83
what is th way to getting the data from the text file with some given time interval.
search.txt
19:00:00 , trakjkfsa,
19:00:00 , door,
19:00:00 , sweater,
19:00:00 , sweater,
19:00:00 , sweater,
19:00:00 , dis,
19:00:01 , not,
19:00:01 , nokia,
19:00:01 , collar,
19:00:01 , nokia,
19:00:01 , collar,
19:00:01 , gsm,
19:00:01 , sweater,
19:00:01 , sweater,
19:00:01 , gsm,
19:00:02 , gsm,
19:00:02 , show,
19:00:02 , wayfreyerv,
19:00:02 , door,
19:00:02 , collar,
19:00:02 , or,
19:00:02 , harman,
19:00:02 , women's,
19:00:02 , collar,
19:00:02 , sweater,
19:00:02 , head,
19:00:03 , womanw,
19:00:03 , com.shopclues.utils.k@42233ff0,
19:00:03 , samsu,
19:00:03 , adidas,
19:00:03 , collar,
19:00:04 , ambas,
i need to find out all the query between time 19:00:00 - 19:00:03 is there any way to find out ?
Upvotes: 0
Views: 93
Reputation: 580
file = open('search.txt')
start = '19:00:02'
end = '19:00:04'
queries = []
line = file.read(10) #read first 10 bytes
while start not in line: #while the first 10 characters are not '19:00:02'
file.readline()
line = file.read(10)
while end not in line:
queries.append(file.readline().strip())
line = file.read(10)
print queries
This reads the first 10 bytes of each line which contains every character up to the comma. If the string 19:00:04
is not in the read string I append the rest of the line with file.readline().strip()
to the queries
list. This is done until the search_for
time is read.
Upvotes: 0
Reputation: 13459
Use the builtin datetime module:
import datetime as dt
t_start = dt.time(19,0,0)
t_end = dt.time(19,0,3)
with open('search.txt') as f:
for line in f:
fields = [ x.strip() for x in line.split(',') ]
timestamp = dt.datetime.strptime(fields[0], "%H:%M:%S").time()
if t_start < timestamp < t_end: # use "<=" if you want to search with boundaries included
print fields[1],
This will print:
not nokia collar nokia collar gsm sweater sweater gsm gsm show wayfreyerv door collar or harman women's collar sweater head
Upvotes: 1