我有一个类似
的文本文件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:,下一个立即值 00: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
所以想到在它的输出中使用相同的方法只得到在这种情况下12
和18
的最后一部分,然后执行减法或差异。但我无法找到只获得最后一部分并执行计算的方法。
我希望输出为
First interaction : 6 seconds
Second interaction : 3 seconds
Third interaction : 3 seconds
等等
答案 0 :(得分: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
答案 1 :(得分:0)
如果你的源文件是以相同的格式,那就是每对内容形成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
答案 2 :(得分:-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