How to unget a line when reading from a file/stream iterator/generator?

George Sakkis george.sakkis at gmail.com
Mon Apr 28 16:16:08 EDT 2008


On Apr 28, 2:07 pm, pyt... at bdurham.com wrote:
> Is there an elegant way to unget a line when reading from a file/stream
> iterator/generator?
>
> By "unget" I mean a way to push back a line into the source stream and
> backtrack the iterator/generator one step?
>
> The only alternative I can see is to put my line reading in a while-True
> loop (vs. a for-loop) that manually calls my file/stream
> iterator/generator's .next() method while manually handling the
> StopIteration exception. Doesn't sound too elegant.
>
> Is there a Pythonic design pattern/best practice that I can apply here?
>
> Thank you,
> Malcolm

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/502304

HTH,
George



More information about the Python-list mailing list