skip item in list "for loop"

Jacob Rael jacob.rael at gmail.com
Fri Apr 14 14:09:32 EDT 2006


I am new to python and I love it. I am hacking a file. I want to not
print a line if it contains the word 'pmos4_highv'. I also don't want
to print the next line. The following code works but it "smells bad"
and just doesn't look right. I was reading about generators. If I was
using one, I could do a .next() after the match and get rid of the
flags. Any suggestions how I can improve this?


inPmos    = False

inFile = sys.stdin
fileList = inFile.readlines()

for line in fileList:
    line = line.rstrip()
    # Remove PMOS definitions - typically two lines. Screwed if only
one and last inst.
    if inPmos:
        inPmos = False
        continue
    if 'pmos4_highv' in line:
        inPmos = True
        continue


jr




More information about the Python-list mailing list