2014-11-25 15 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