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)

运行结果:

相关推荐
API快乐传递者1 分钟前
除了网页标题,还能用爬虫抓取哪些信息?
开发语言·爬虫·python
豌豆花下猫9 分钟前
REST API 已经 25 岁了:它是如何形成的,将来可能会怎样?
后端·python·ai
平头哥在等你2 小时前
Python中的正则表达式教程
python·正则表达式
Best_Me072 小时前
如何在Pycharm的终端里进入自己的环境
ide·python·pycharm
好看资源平台3 小时前
爬虫开发工具与环境搭建——环境配置
爬虫·python
大G哥3 小时前
python 数据类型----可变数据类型
linux·服务器·开发语言·前端·python
赛丽曼3 小时前
Python中的HTML
python·html
luky!3 小时前
算法--解决熄灯问题
python·算法
深度学习lover4 小时前
<项目代码>YOLOv8 番茄识别<目标检测>
人工智能·python·yolo·目标检测·计算机视觉·番茄识别
IT古董4 小时前
【机器学习】机器学习中用到的高等数学知识-1.线性代数 (Linear Algebra)
人工智能·python·线性代数·机器学习