Skip to main content
 首页 » 编程设计

按特定时间过滤日志文件的Python代码

2025年02月15日13mq0036

我在编写下一段代码时遇到了问题:

from datetime import datetime, timedelta 
def find_last_index(file_rec): 
    time = datetime.now() - timedelta(hours=2) 
    file_content = file_rec 
    while True: 
        ind = file_content.find(time.strftime("%m-%d")) 
        date_obj = datetime.strptime(file_content[ind:13], '%m-%d %H:%M:%S') 
        if time.hour > date_obj.hour: 
            file_content = file_content[ind+5:] 
            ind = file_content.find("12-22", ind) 
            return ind 
        else: 
            file_content = file_content[ind + 1:] 
 
 
file_name = raw_input("Enter File Path From this file's dir: ") 
read_file = open(file_name, 'r') 
content = read_file.read() 
read_file.close() 
lastindex = find_last_index(content) 
print content[:lastindex] 
content = input() 
write_file = open("ResultFile.txt", "w") 
write_file.write(content[:lastindex]) 
write_file.close() 

代码应该采用如下所示的日志文件:

12-22 20:14:15.972 26560 27796 D Robocol : no packet received: NullPointerException 
12-22 20:14:15.972 26560 27796 D Robocol : no packet received: NullPointerException 
12-22 20:14:15.972 26560 27796 D Robocol : no packet received: NullPointerException 
12-22 20:14:15.972 26560 27796 D Robocol : no packet received: NullPointerException 
12-22 20:14:15.973 26560 27796 D Robocol : no packet received: NullPointerException 
12-22 20:14:15.973 26560 27796 D Robocol : no packet received: NullPointerException 
12-22 20:14:15.973 26560 27796 D Robocol : no packet received: NullPointerException 
12-22 20:14:15.973 26560 27796 D Robocol : no packet received: NullPointerException 
12-22 20:14:15.973 26560 27796 D Robocol : no packet received: NullPointerException 
12-22 20:14:15.974 26560 27796 D Robocol : no packet received: NullPointerException 
12-22 20:14:15.974 26560 27796 D Robocol : no packet received: NullPointerException 
12-22 20:14:15.974 26560 27796 D Robocol : no packet received: NullPointerException 
12-22 20:14:15.9 

每一行都以日期和时间开头。我只想将 2 小时前到当前时间的语句插入到新文件中。 如果有人能帮助我解决它,那就太棒了。

请您参考如下方法:

我忍不住建议使用 arrow 模块来处理日期。在许多情况下,它使生活更轻松。这是我提供的。

>>> import arrow 
>>> refTime = arrow.now().shift(hours=-2).strftime('%m-%d %H:%M:%S') 
>>> refTime 
'12-27 13:13:57' 
>>> str(refTime) 
'12-27 13:13:57' 
>>> refTime_as_str = str(refTime) 
>>> with open('logfile.txt') as log: 
...     for line in log: 
...         if line[:len(refTime_as_str )] >= refTime_as_str: 
...             print (line.strip()) 
...              
12-27 15:45:50.972 26560 27796 D Robocol : no packet received: NullPointerException 
12-27 15:45:50.972 26560 27796 D Robocol : no packet received: NullPointerException 
12-27 15:45:50.972 26560 27796 D Robocol : no packet received: NullPointerException 
12-27 15:45:50.972 26560 27796 D Robocol : no packet received: NullPointerException 
12-27 15:45:50.973 26560 27796 D Robocol : no packet received: NullPointerException 
12-27 15:45:50.973 26560 27796 D Robocol : no packet received: NullPointerException 
12-27 15:45:50.973 26560 27796 D Robocol : no packet received: NullPointerException 
12-27 15:45:50.973 26560 27796 D Robocol : no packet received: NullPointerException 
12-27 15:45:50.973 26560 27796 D Robocol : no packet received: NullPointerException 
12-27 15:45:50.974 26560 27796 D Robocol : no packet received: NullPointerException 
12-27 15:45:50.974 26560 27796 D Robocol : no packet received: NullPointerException 
12-27 15:45:50.974 26560 27796 D Robocol : no packet received: NullPointerException