如何使用Python 3在两个时间戳之间打印线?

时间:2015-11-02 10:28:09

标签: python python-3.x

如何过滤掉两个时间戳之间的行并仅在Python中打印这些行?

示例:我想过滤08:00:00到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 个答案:

答案 0 :(得分: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