如何使用Python 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)
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
是的,这是工作@ravichandra非常感谢! – RAM
我得到了一个新的场景,如果日期是字符串格式说 例如:星期一5月03 12:23:11: 星期二04五月04 11:00:10: 我希望日期和时间都需要过滤和打印必要的行 – RAM
根据我更新,你将从5月3日到6日从08:00到16:59' – Ravichandra
提供样本的输入数据。请阅读[this](http://stackoverflow.com/help/mcve) – Pynchia
并且比*“它不工作”更具体*。 – jonrsharpe