【问题标题】:Python read specific lines of text between two stringsPython 读取两个字符串之间的特定文本行
【发布时间】: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 没有显示时。有什么更好的方法?

【问题讨论】:

    标签: python text readlines


    【解决方案1】:

    稍作修改,看起来应该可以解决您的问题:

    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
    

    【讨论】:

    • 我喜欢 :) 我明天试试这个
    • 或者,您可以在 parsing = True 之后放置一个 continue 语句,以不解析 '***Report 1****' 行。
    • @mgilson :虽然我同意继续循环是低效的......你为什么建议continue而不是break
    【解决方案2】:

    这是使用 itertools 模块的可能替代方案。
    虽然这里的问题需要检查 [key],但我还添加了 itertool.islice() 以表明当用户有一些先验知识时,可以在 start-reading marker 之后跳过几行信息。

    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)
    

    【讨论】:

      猜你喜欢
      • 2018-12-07
      • 2021-12-11
      • 1970-01-01
      • 1970-01-01
      • 2020-08-09
      • 2023-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多