2015-11-02 85 views
-3

如何過濾兩個時間戳之間的線條並僅在Python中打印這些線條?如何使用Python 3在兩個時間戳之間打印行?

例如:我想過濾08:00:00 am至05:00:00 pm我嘗試了一些這樣的事情,但它不起作用。

def ipaddr(w): 
    print("====Welcome to HTTP log Debugger ==== ") 
    file= input("Please Enter log File path: ") 
    start_time = input("start Time: ") 
    End_time = input("End Time : ") 
    with open(file,'r') as f: 
     lines = f.read().splitlines() 
    for L in lines: 
     if L.startswith(start_time): 
      p= 1 
     elif L.startswith(End_time): 
      p=0 
      break 
     if p: print(L) 
+1

提供樣本的輸入數據。請閱讀[this](http://stackoverflow.com/help/mcve) – Pynchia

+0

並且比*「它不工作」更具體*。 – jonrsharpe

回答

0
D1 = 1 #starting date 

D2 = 10 # for ending date 

clock1 = 8 # for 8'O clock 
clock2 = 17 # for up to 17:59::59 

month_int = 1 # for jan 
Clock= "{0:0=2d}".format(D1) 

for i in range(clock1+1,clock2): 
    Clock = Clock+"|"+"{0:0=2d}".format(i) 


import calendar 
month = calendar.month_name[month_int] 

s= "{0:0=2d}".format(D1) 

for i in range(D1+1,D2): 
    s = s+"|"+"{0:0=2d}".format(i) 

with open('logfile.log', 'r') as f: 
    for line in f: 
      if re.match("\D{3}\s"+month+"\s("+s+")\s("+Clock+")", line): 
       print line 
+0

是的,這是工作@ravichandra非常感謝! – RAM

+0

我得到了一個新的場景,如果日期是字符串格式說 例如:星期一5月03 12:23:11: 星期二04五月04 11:00:10: 我希望日期和時間都需要過濾和打印必要的行 – RAM

+0

根據我更新,你將從5月3日到6日從08:00到16:59' – Ravichandra

相關問題