[snip]les> suppose I am reading lines from a file or stdin. I want to just les> "peek" in to the next line, and if it starts with a special les> character I want to break out of a for loop, other wise I want to les> do readline().
Create a wrapper around the file object:
Of course, this could be tested (which my example hasn't been)
If you'd like something that works similarly and has been tested a bit, try one of my recipes for peeking into an iterator:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/304373
You also might consider writing an iterator wrapper for a file object:
>>> class strangefileiter(object): ... def __init__(self, file): ... self.itr = iter(file) ... def __iter__(self): ... while True: ... next = self.itr.next() ... if not next or next.rstrip('\n') == "|": ... break ... yield next ... >>> file('temp.txt', 'w').write("""\ ... some text ... some more ... | ... not really text""") >>> for line in strangefileiter(file('temp.txt')): ... print repr(line) ... 'some text\n' 'some more\n' >>> file('temp.txt', 'w').write("""\ ... some text ... some more ... ... really text""") >>> for line in strangefileiter(file('temp.txt')): ... print repr(line) ... 'some text\n' 'some more\n' '\n' 'really text'
Steve -- http://mail.python.org/mailman/listinfo/python-list