简体   繁体   中英

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. 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:

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

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. 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:

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

You can neatly solve it in one line using the RX operators such as:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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