skip item in list "for loop"
Diez B. Roggisch
deets at nospam.web.de
Fri Apr 14 14:33:50 EDT 2006
Jacob Rael schrieb:
> 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?
A generator certainly would be handy:
def read_lines(inFile):
fg = iter(inFile)
for line in fg:
if "pmos4_highv" in line:
fg.next()
else:
yield line
lines = """a
b
c
d
pmos4_highv
no no!
e
f
g""".split("\n")
for line in read_lines(lines):
print line
Diez
More information about the Python-list
mailing list