I have a text file which has something like
00:47:12: start interaction
00:47:18: End interaction
00:47:20: Start interaction
00:47:23: End interaction
00:47:25: Start interaction
00:47:28: End interaction
00:47:29: Start interaction
00:47:31: End interaction
I would like to get the time stamp value from the file like 00:47:12: and the next immediate value here 00:47:18: and find the time difference between the values in this case 6 seconds and print as the output. Would be great to have some possible suggestions. I tried to implement the first part of getting the seconds value but I am stuck here.
Code:
with open('Time_delay', 'r') as time_delay:
for line in time_delay:
time_stamp = re.findall(r"\:(.*?)\: ",line)
time_stamp = ''.join(time_stamp)
#time_stamp = re.findall(r"\:(.*?)\: ",str(time_stamp))
#time_stamp = ''.join(time_stamp)
print line
print str(time_stamp)
The first re.findall
prints
47:12
47:18
SO thought of using the same method for the output of it to get only the last part which is 12
and 18
in this case and then perform the subtraction or difference. But I am unable to find the way to get only the last part and perform the calculation.
I want my output as
First interaction : 6 seconds
Second interaction : 3 seconds
Third interaction : 3 seconds
and so on
you can try like this using
datetime
moduleif your file is like this:
code here:
to handle empty lines: