我编写的程序将日志(log)文件中的数据打印到图形用户界面.它通过 Select 一些特征来执行过滤操作.我正在逐行获取数据 我可以根据日期和时间格式取出带有单词的行.

但我想要的是拿到我想要的线上的线. 当我在词条中键入5时,我希望我想要搜索的单词位于上面5行.

例如,我的单词是‘定时器’.当我在条目中写入计时器,并选中在行之前复选框并在之前行条目中写入5时.我想拿着这个;

[01/01/70 02:00:18.699984 ] [debug  ] [1403] [DmTr069EventHandler.c:55] [dmTr069EventHandler_init] LEAVED 
[01/01/70 02:00:18.700122 ] [debug  ] [1403] [DmUkaEventHandler.c:50] [dmUkaEventHandler_init] ENTERED 
[01/01/70 02:00:18.700143 ] [debug  ] [1403] [DmUkaEventHandler.c:52] [dmUkaEventHandler_init] LEAVED 
[01/01/70 02:00:18.700154 ] [debug  ] [1403] [DmAppEventHandler.c:81] [dmAppEventHandler_init] ENTERED 
[01/01/70 02:00:18.700237 ] [debug  ] [1403] [Timer.c:441] [addTimerToSortedTimerList] ENTERED 

密码在这里.我try 了一些方法,但对之前的功能不起作用.

def search(msg, startingDate, endingDate, beforeLine, varBefore):
# clear current result
text.delete('1.0', 'end')
with open('OAM.log', 'r', encoding='latin1') as fp:
    global l_no
    for l_no, line in enumerate(fp, 1):
        if msg and msg not in line:
            # does not contain search message, skip it
            continue
        if startingDate or endingDate:
            # get the timestamp
            timestamp = parse_date(line[1:25])
            # within startingDate and endingDate ?
            if startingDate and timestamp < startingDate:
                # before given starting date, skip it
                continue
            if endingDate and timestamp > endingDate:
                # after given ending date, skip it
                continue

        """for count, beforeLine in enumerate(fp, 1):
            #bfline = fp.readlines(l_no - count)
            count -= 1
            text.insert('end', ' \n ')
            text.insert('end', f'Before Line Number: {l_no - beforeEntryVar.get()} Log: {beforeLine}')
            text.insert('end', ' \n ')"""
            
        # insert the log
        text.insert('end', ' \n ')
        text.insert('end', f'Line Number: {l_no} Log: {line}')
        text.insert('end', ' \n ')

推荐答案

要做到这一点,最简单的方法是使用双排,如下所示:

import re
from collections import deque

def read_logfile(filename, key, maxlines):
    p = r'(?<=\[).+?(?=\])'
    d = deque([], maxlines)
    with open(filename) as log:
        for line in map(str.strip, log):
            d.append(line)
            if len(m := re.findall(p, line)) > 3 and m[3].startswith(key):
                return d
    return []

print(*read_logfile('OAM.log', 'Timer', 5), sep='\n')

Python相关问答推荐

如何判断LazyFrame是否为空?

如何从格式为note:{neighbor:weight}的字典中构建networkx图?

如何将新的SQL服务器功能映射到SQL Alchemy的ORM

使用Python Cerberus初始化一个循环数据 struct (例如树)(v1.3.5)

是pandas.DataFrame使用方法查询后仍然排序吗?

按照行主要蛇扫描顺序对点列表进行排序

在matplotlib动画gif中更改配色方案

在Python中为变量的缺失值创建虚拟值

Python -根据另一个数据框中的列编辑和替换数据框中的列值

如何才能知道Python中2列表中的巧合.顺序很重要,但当1个失败时,其余的不应该失败或是0巧合

@Property方法上的inspect.getmembers出现意外行为,引发异常

Pandas 在最近的日期合并,考虑到破产

将特定列信息移动到当前行下的新行

将jit与numpy linSpace函数一起使用时出错

运行总计基于多列pandas的分组和总和

Polars:用氨纶的其他部分替换氨纶的部分

driver. find_element无法通过class_name找到元素'""

多指标不同顺序串联大Pandas 模型

幂集,其中每个元素可以是正或负""""

查看pandas字符列是否在字符串列中