skip item in list "for loop"

Diez B. Roggisch deets at nospam.web.de
Fri Apr 14 16:57:36 EDT 2006


Felipe Almeida Lessa schrieb:
> Em Sex, 2006-04-14 às 20:33 +0200, Diez B. Roggisch escreveu:
>> def read_lines(inFile):
>>      fg = iter(inFile)
>>      for line in fg:
>>          if "pmos4_highv" in line:
>>              fg.next()
>>          else:
>>              yield line
> 
> Just be aware that the "fb.next()" line can raise an StopIteration
> exception that would otherwise be caught by the for loop. If you have
> any resources that need to be cleaned up, try...finally is you friend.

Actually I did that on purpose - as the StopIteration would simply end 
the generator. Any clean-up - well, if that was the scope of the 
generator on could add it of course, but I'd do that on the same level 
the file has been opened.

Regards,

Diez



More information about the Python-list mailing list