简体   繁体   English

Python在找到特定行后读取行

[英]Python read lines after finding specific lines

I am trying to take out the certain lines after a certain line is being found. 找到某行后,我试图取出某些行。 Below is the example: 下面是示例:

 1. ABC01
 2. AB_Name
 3. AC_Name
 4. ID_Name
 5. ABC02
 6. AB_Name
 7. ABB_Name
 8. AC_Name
 9. AQ_Name
 10. ID_Name
 11. ABC01
 12. AP_Name
 13. AZ_Name
 14. AB_Name
 15. ID_Name

What I am trying to take out is the everysingle line that goes after ABC01 and ignore ABC02 and line after it. 我要删除的是ABC01之后的单行,而忽略ABC02及其之后的行。 So the output that I am looking for is: 所以我正在寻找的输出是:

 1. ABC01
 2. AB_Name
 3. AC_Name
 4. ID_Name

 11. ABC01
 12. AP_Name
 13. AZ_Name
 14. AB_Name
 15. ID_Name

I have tried if statements like: 我已经尝试过if语句,例如:

lines = [line.rstrip('\n') for line in open('File.txt')]

listings = []

for line in lines:

    if line.startswith("ABC01"):
        continue
    if line.startswith("ID"):
        break

    listings.append(line.strip())

I am using Python 2.7 我正在使用Python 2.7

I'd just keep a flag that lets me keep track of what to ignore and what to take. 我只是保留一个标志,让我可以跟踪要忽略的内容和要采取的内容。

ignore = False

for line in lines:
    if line.startswith("ABC01"):
        ignore = False
    elif line.startswith("ABC02"):
        ignore = True

    if not ignore:
        listings.append(line.strip())

You need to recognize the patterns you want to capture (ABC01 and afterwards), and stop capturing when the other pattern is found. 您需要识别要捕获的模式(ABC01及更高版本),并在找到其他模式时停止捕获。 You can do that with a flag . 您可以使用标志来实现 We turn on the flag when we find the pattern, and indicate the next lines should be captured. 找到模式后,我们将打开标记,并指示应捕获下一行。 We turn the flag to False when the ABC02 pattern is found: 找到ABC02模式后,我们将标志设置为False

lines = ['ABC01', 'AB_Name', 'AC_Name', 'ID_Name',
          'ABC02', 'AB_Name', 'ABB_Name', 'AC_Name',
          'AQ_Name', 'ID_Name', 'ABC01', 'AP_Name',
          'AZ_Name', 'AB_Name', 'ID_Name']

get_lines = False
output = []

for line in lines:
    if line.startswith('ABC01'):
        get_lines = True

    elif line.startswith('ABC02'):
        get_lines = False
        continue

    if get_lines:
        output.append(line)

I would attack this problem using Pyhton ReactiveX extensions: https://github.com/ReactiveX/RxPY 我会使用Pyhton ReactiveX扩展来解决此问题: https : //github.com/ReactiveX/RxPY

You can neatly solve it in one line using the RX operators such as: 您可以使用RX运算符在一行中巧妙地解决它,例如:

Observable.from_(lines).skip_while(lambda line: "ABC01" in line ).filter(lambda line: not "ABC02" in line).subscribe(lambda line: print(line))

Disclaimer: code not tested, but i think you got the idea! 免责声明:代码未经测试,但我认为您明白了!

Charlie 查理

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM