Subtracting an integer value from a text file and displaying the result in Python2.7

1k views Asked by At

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

3

There are 3 answers

0
Hackaholic On BEST ANSWER

you can try like this using datetime module

if your file is like this:

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

code here:

>>> f = open('file.txt')
>>> for x in f:
...     start = x.split()[0][:-1]
...     end = f.next().split()[0][:-1]
...     print str(datetime.datetime.strptime(end,"%H:%M:%S")- datetime.datetime.strptime(start,"%H:%M:%S")).split(':')[-1]
... 
06
03 
03
02

to handle empty lines:

>>> f = open('file.txt').readlines()
>>> my_file = [ x for x in f if x!='\n' ]
>>> for x in range(0,len(my_file)-1,2):
...     start = my_file[x].split()[0][:-1]
...     end = my_file[x+1].split()[0][:-1]
...     print str(datetime.datetime.strptime(end,"%H:%M:%S")- datetime.datetime.strptime(start,"%H:%M:%S")).split(':')[-1]
... 
06
03
03
02
0
JonB On

If your source file is consisently in the same format, that is each pair of lines with content form a start / end group, this will work. It even accounts for blank lines.

from datetime import datetime

def calcTimes(file):
    with open(file, 'r') as f:
        parsedTimeArray = [line.split(': ')[0] for line in f if len(line.rstrip('\n')) != 0]
    format = '%H:%M:%S'
    for t in range(0,(len(parsedTimeArray)-1),2):
        timeStart = datetime.strptime(parsedTimeArray[t],   format)
        timeEnd   = datetime.strptime(parsedTimeArray[t+1], format)
        print str(int((timeEnd - timeStart).total_seconds()))

calcTimes('Time_delay')

Result:

6
3
3
2
2
Mazdak On

If you want to get the last element you can use look-behind in regex :

>>> s = '00:47:12: start interaction'
>>> re.search(r'(?<=\d{2}\:\d{2}\:)\d+',s).group(0)
'12'

and then convert it to int , then calculate the difference !

edit : as you may check the empty lines too you need to use an if :

 if re.search(r'(?<=\d{2}\:\d{2}\:)\d+',s) :
                print re.search(r'(?<=\d{2}\:\d{2}\:)\d+',s).group(0)

Also as another way you can split your lines and convert the string time to time :

>>> sp_line1= re.split(r'(?<=\d{2}:\d{2}:\d{2}):',s)
['00:47:12', ' start interaction']

Demo :

>>> t1= strptime(sp_line1[0],"%H:%M:%S")
>>> s2="00:47:18: End interaction"
>>> sp_line1=re.split(r'(?<=\d{2}:\d{2}:\d{2}):',s2)
>>> sp_line2=re.split(r'(?<=\d{2}:\d{2}:\d{2}):',s2)
>>> t2= strptime(sp_line2[0],"%H:%M:%S")
>>> t1.tm_sec
12
>>> t2.tm_sec - t1.tm_sec
6