【发布时间】:2012-07-28 18:19:21
【问题描述】:
我无法让 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 没有显示时。有什么更好的方法?
【问题讨论】: