2014-11-25 46 views
0

显示结果我有具有类似减去从文本文件的整数值,在Python2.7

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 

一个文本文件,我想从文件中获得时间戳值一样00 :47:12:和这里的下一个立即值○时47分18秒:和找到的值之间的时间差在这种情况下6秒和打印作为输出。会有一些可能的建议很好。我试图实现获取秒值的第一部分,但我卡在这里。

代码:

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) 

第一re.findall打印

47:12 
47:18 

SO认为使用它的输出同样的方法来获取只有最后一部分是1218在这种情况下然后执行减法或差分。但是我无法找到获取最后部分并执行计算的方法。

我希望我的输出

First interaction : 6 seconds 
Second interaction : 3 seconds 
Third interaction : 3 seconds 

+1

你为什么不把它们转换成'time' /'datetime'对象然后只减去他们? – jonrsharpe 2014-11-25 12:50:07

+0

@jonrsharpe我对datetime对象很新,如果你可以提供一个示例实现,这会有很大的帮助吗? – user89 2014-11-25 12:56:11

+0

https://docs.python.org/2/library/datetime.html – jonrsharpe 2014-11-25 12:57:02

回答

-1

你可以尝试这样的使用datetime模块

,如果你的文件是这样的:

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 

代码在这里:

>>> 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 

处理空行:

>>> 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

如果你想,你可以在正则表达式使用look-behind最后一个元素:

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

,然后将其转换为int,然后计算差异!

编辑:你可以检查空行过,你需要使用一个if

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

另外,作为另一种方式,你可以分割你的线条和字符串时间转换时间:

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

演示:

>>> 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 
+0

尝试第一个选项时出现错误。 'TIME_STAMP = re.findall(R “\:(*)\:?”,线) \t \t \t \t \t TIME_STAMP = ''。加入(TIME_STAMP) \t \t \t \t \t TIME_STAMP = STR(TIME_STAMP) \t \t \t \t \t time_stamp_start = re.search(r'(?<= \ d {2} \:\ d {2} \:)\ d +',time_stamp).group(0)'错误是** AttributeError :'NoneType'对象没有属性'group'** – user89 2014-11-25 13:32:47

+0

@VenkateshPadmanabhan它由于空行('\ n')而只需要一个异常,请参阅编辑 – Kasramvd 2014-11-25 14:08:33

0

如果源文件是consisently在相同的格式,即每对内容形成一个start/end组的行,这将工作。它甚至占了空白的行。

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') 

结果:

6 
3 
3 
2