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)

运行结果:

相关推荐
孟健12 小时前
Karpathy 用 200 行纯 Python 从零实现 GPT:代码逐行解析
python
码路飞14 小时前
写了个 AI 聊天页面,被 5 种流式格式折腾了一整天 😭
javascript·python
曲幽16 小时前
FastAPI压力测试实战:Locust模拟真实用户并发及优化建议
python·fastapi·web·locust·asyncio·test·uvicorn·workers
敏编程21 小时前
一天一个Python库:jsonschema - JSON 数据验证利器
python
前端付豪21 小时前
LangChain记忆:通过Memory记住上次的对话细节
人工智能·python·langchain
databook21 小时前
ManimCE v0.20.1 发布:LaTeX 渲染修复与动画稳定性提升
python·动效
花酒锄作田1 天前
使用 pkgutil 实现动态插件系统
python
前端付豪2 天前
LangChain链 写一篇完美推文?用SequencialChain链接不同的组件
人工智能·python·langchain
曲幽2 天前
FastAPI实战:打造本地文生图接口,ollama+diffusers让AI绘画更听话
python·fastapi·web·cors·diffusers·lcm·ollama·dreamshaper8·txt2img
老赵全栈实战2 天前
Pydantic配置管理最佳实践(一)
python