我无法让 python 读取特定的行。我正在做的是这样的:
lines of data not needed
lines of data not needed
lines of data not needed
--------------------------------------
***** REPORT 1 *****
--------------------------------------
[key] lines of interest are here
[key] lines of interest are here
[key] lines of interest are here
[key] lines of interest are here
[key] lines of interest are here #This can also be the EOF
--------------------------------------
***** REPORT 2 *****
--------------------------------------
lines of data not needed
lines of data not needed
lines of data not needed #Or this will be the EOF
我尝试过的是:
flist = open("filename.txt").readlines()
for line in flist:
if line.startswith("\t**** Report 1"):
break
for line in flist:
if line.startswith("\t**** Report 2"):
break
if line.startswith("[key]"):
#do stuff with data
但是,当文件结束时没有结束分隔符时,我遇到了问题......例如不显示报告 #2 时。什么是更好的方法?