我正在尝试打印文本文件中匹配文本之后的行。
类似这样的东西:
import re
afterlines=3
with open(filename, 'r') as f:
for line in f:
if line.strip()== ls_losses:
row = f.readline(+afterlines)
print (row)
print ("true")
答案 0 :(得分:0)
我只会使用一个临时计数器。
import re
afterlines=3
temporary_lines = ''
with open(filename, 'r') as f:
for line in f:
if line.strip() == ls_losses:
counter = afterlines
if counter > 0:
temporary_lines += f.readline()
counter -= 1
else:
print(temporary_lines)
temporary_lines = '' # Reinitialize to get ready for the next match
print ("true")