how do I "peek" into the next line?

Steven Bethard steven.bethard at gmail.com
Mon Dec 13 22:51:33 CET 2004


I wrote:
> ...         while True:
> ...             next = self.iter.peek()
> ...             if not next or next.rstrip('\n') == "|":
> ...                 break
> ...             yield self.iter.next()
> ...

Actually, the 'not next' test is not necessary since I'm using an 
iterator over the file (end of file is signified by StopIteration, not a 
value of '' returned by the iterator).  The amended version:

...         while True:
...             next = self.iter.peek()
...             if next.rstrip('\n') == "|":
...                 break
...             yield self.iter.next()

Steve



More information about the Python-list mailing list