how do I "peek" into the next line?

Craig Ringer craig at postnewspapers.com.au
Mon Dec 13 15:38:46 EST 2004


On Tue, 2004-12-14 at 03:09, les_ander at yahoo.com wrote:
> Hi,
> suppose I am reading lines from a file or stdin.
> I want to just "peek" in to the next line, and if it starts
> with a special character I want to break out of a for loop,
> other wise I want to do readline().

Assuming there's a good reason, such as monster lines, not to just read
the next line anyway, I'd suggest read()ing the next character then
seek()ing back by one character to restore the file position.

def peekChar(fileobj):
   ch = fileobj.read(1)
   fileobj.seek(-1,1)
   return ch

--
Craig Ringer




More information about the Python-list mailing list