from collections import deque

def search(lines, pattern, history=5):
previous_lines = deque(maxlen=history)
for line in lines:
if pattern in line:
yield line,previous_lines
previous_lines.append(line)

if __name__ == '__main__':
with open('log.txt','r',encoding='utf-8') as f:
for line, prevlines in search(f, 'return', 1):
for pline in prevlines:
print(pline,end='')
print(line,end='')
print ('-'*20)

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-08-09
  • 2021-06-20
  • 2021-10-26
  • 2021-06-19
猜你喜欢
  • 2021-07-13
  • 2022-12-23
  • 2021-09-08
  • 2022-01-02
相关资源
相似解决方案