Python读取某个时间点之后的日志

python 复制代码
import time
import re

def date_time_float(time_str, time_format=None):
    time_stamp = 0
    if len(time_str) > 24:
        time_str = time_str[:24]
    try:
        if time_format is None:
            time_format = '%Y-%m-%d %H:%M:%S'
            if 'T' in time_str:
                time_format = '%Y-%m-%dT%H:%M:%SZ'
        ms_float_time = 0
        s_time = time_str.split('.')[0]
        if '.' in time_str:
            ms_float_time = float(time_str.split('.')[-1]) / 1000.0
        time_stamp = time.mktime(time.strptime(s_time, time_format)) + ms_float_time
    except:
        pass
    return time_stamp

temp_str = '''2024-07-17 17:51:16.790 <142> [2024-7-17 17:51:23.91844][PID2615][ffffffff][L2S][info]L2S_RrcProcPhyFraNumInfo: 876:[0,1207,17908]  PhyFraNum:0 next pos
2024-07-17 17:51:17.212 <134> [2024-7-17 17:51:23.522884][PID2652][ffffffff][OM][info]FPGAM_Task:1772:[0,1207,18339]  FPGAM Module resaved message!
2024-07-17 17:51:18.366 <142> [2024-7-17 17:51:24.671879][PID2615][ffffffff][NAS][info]nas_timer_stop: 301:[0,1207,19488]  MM-PROC  - Stop timer id (998638)
2024-07-17 17:51:19.219 <134> [2024-7-17 17:51:25.208775][PID2641][10000016][MSGTRACE][info]PFM_MsgTask:1867:[0,1207,20025]  TASK_OAM_PFM Received message 0x1000
2024-07-17 17:51:20.383 <142> [2024-7-17 17:51:26.691849][PID2615][ffffffff][NAS][info]nas_timer_stop: 301:[0,1207,21508]  MM-PROC  - Stop timer id (1118223)
2024-07-17 17:51:21.397 <139> [2024-7-17 17:51:27.701906][PID2615][0][MOS][error]MOS_TimerDelete: 496:[0,1207,22518]  TIMER ID 1194679 ERROR! not match 0
2024-07-17 17:51:22.406 <139> [2024-7-17 17:51:28.711897][PID2615][0][MOS][error]MOS_TimerDelete: 496:[0,1207,23528]  TIMER ID 1300996 ERROR! not match 0
2024-07-17 17:51:27.642 <142> [2024-7-17 17:51:28.712523][PID2615][ffffffff][NAS][info]nas_timer_start: 268:[0,1207,23529]  create nas timer (1373732)'''

ori_list = temp_str.split('\n')
start_time = '2024-07-17 17:51:21.397'
start_time_stamp = date_time_float(start_time)
dst_list = []
for line in ori_list:
    pattn = re.compile('(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}.\d{3})')
    crt_line_time = re.findall(pattn, line)
    if crt_line_time:
        time_stamp = date_time_float(crt_line_time[0])
        if start_time_stamp <= time_stamp:
            print(line)

运行结果:

相关推荐
豆芽81910 分钟前
决策树(DecisionTree)
python·决策树·机器学习·pyqt·sklearn
努力犯错玩AI12 分钟前
Llama 4 来了!AI 快站助你一键极速下载,抢先体验 MoE + 多模态 + 超长上下文
人工智能·后端·python
eqwaak017 分钟前
DrissionPage高级技巧:从爬虫到自动化测试
人工智能·爬虫·python·语言模型·自然语言处理·drissionpage
创新技术阁38 分钟前
FastAPI核心技巧大公开:深入探索路由与视图的奥秘
后端·python
Blueshy39 分钟前
【python基础】weakref的初次遇见
python
前端开发张小七1 小时前
11.Python设计模式:单例模式与工厂模式实战指南
前端·python
前端开发张小七1 小时前
10. Python闭包:优雅的状态封装与实用技巧
python
ENE1 小时前
WIN11 24H2关闭基于虚拟化安全
windows
fc&&fl1 小时前
AI爬虫?爬!
人工智能·爬虫·python
zidea1 小时前
我和我的 AI Agent(2)工具模块设计上花了不少心思,实现了 AI Agent 从使用工具到创建工具的进化
人工智能·python·程序员