我编写的程序将日志(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 ')