简体   繁体   中英

How to skip n number of lines after finding a specific line in Python 3?

Let's say I have a large text file, and I want to skip a line containing some keyword, as well as 2 lines after that line.

Original_file:

line1 some words
line2 some words
line3 keyword
line4 some words
line5 some words
line6 some words
line7 some words
line8 some words

New_file:

line1 some words
line2 some words
line6 some words
line7 some words
line8 some words

A simplified snippet of my code:

with open('Original_file','r') as f:
    lines = f.readlines()
    nf = open('New_file', 'w')
    for line in lines:
        if 'keyword' in line:
            for i in range(3): continue
        else:
            nf.write(line + "\n")

The loop, "for i in range(3): continue" doesn't skip lines (I assume because it just continues within that nested for loop instead of the "for line in lines" for loop. I also tried "next(f)" instead of "continue" and got a StopIteration error message.

Of course, if I try,

with open('Original_file','r') as f:
    lines = f.readlines()
    nf = open('New_file', 'w')
    for line in lines:
        if 'keyword' in line: 
            continue
        else:
            nf.write(line + "\n")

it succeeds in skipping a line, but only the line with the keyword, whereas I want to skip the next two lines as well (a total of 3 lines).

Any suggestions are appreciated. Thank you for your help.

Make a counter and skip lines based off that.

with open('Original_file','r') as f:
    lines = f.readlines()
    nf = open('New_file', 'w')
    skip_lines = 0
    for line in lines:
        if skip_lines > 0:
            skip_lines -= 1
        elif 'keyword' in line: 
            skip_lines = 3
        else:
            nf.write(line + "\n")

You can achieve this with a flag /counter

with open('Original_file','r') as f:
    lines = f.readlines()
    skip = 0
    nf = open('New_file', 'w')
    for line in lines:
        if skip:
            skip -=1
        elif 'keyword' in line:
            skip = 3
        else:
            nf.write(line + "\n")

You can try iterate over indexes instead of elements:

with open('Original_file','r') as f:
    lines = f.readlines()

nf = open('New_file', 'w')
i = 0
while i< len(lines):
    line = lines[i]
    if 'keyword' in line:
        i+=3
    else:
        nf.write(line + "\n")
        i+=1

You can use next() , ie:

with open("old.txt") as f, open("new.txt", "w") as w:
  for line in f:
    if "keyword" in line:
      next(f), next(f)
      continue
    w.write(line)

Demo


If you prefer a list comprehension , you can also use:

with open("old.txt") as f, open("new.txt", "w") as w:
  [w.write(line) if not "keyword" in line else [next(f) for _ in range(2)] for line in f]

Demo

The problem is that you extract lines directly from a list and because of that, you have no possible interaction with the underlying iterator used by for line in lines . You should simply use the file object as an iterator:

with open('Original_file','r') as f, open('New_file', 'w') as nf
    for line in f:
        if 'keyword' in line:
            for i in range(2): next(f)
        else:
            nf.write(line)

The only assumption here is that any line containing keyword is followed with at least 2 other lines.

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