У меня возникли проблемы с тем, чтобы 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 не отображается. Какой подход лучше?