5

我无法让 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 时。什么是更好的方法?

4

2 回答 2

10

一项看起来应该可以解决您的问题的轻微修改:

flist = open("filename.txt").readlines()

parsing = False
for line in flist:
    if line.startswith("\t**** Report 1"):
        parsing = True
    elif line.startswith("\t**** Report 2"):
        parsing = False
    if parsing:
        #Do stuff with data 

如果您想避免解析“ * Report 1”...行本身,只需将开始条件放在 之后if parsing,即

flist = open("filename.txt").readlines()

parsing = False
for line in flist:

    if line.startswith("\t**** Report 2"):
        parsing = False
    if parsing:
        #Do stuff with data 
    if line.startswith("\t**** Report 1"):
        parsing = True
于 2012-07-31T02:51:55.383 回答
1

这是使用 itertools 模块的可能替代方法。
尽管这里的问题需要检查 [key],但我还添加了 itertool.islice() 以表明当用户有一些先验信息时,可以在开始阅读标记之后跳过几行。

from itertools import takewhile, islice, dropwhile

with open('filename.txt') as fid:
    for l in takewhile(lambda x: '***** REPORT 2 *****' not in x, islice(dropwhile(lambda x: '***** REPORT 1 *****' not in x, fid), 1, None)):
        if not '[key]' in l:
            continue
        print(l)
于 2019-08-29T17:21:18.757 回答