Seeing next character in an file

R.Marquez ny_r_marquez at yahoo.com
Mon Jul 28 16:25:38 CEST 2003


Grumfish <nobody at nowhere.com> wrote in message news:<CDFUa.42241$wk4.27296 at twister.nyroc.rr.com>...
> Is there a way to see the next character of an input file object without 
> advancing the position in the file?

Here is a little class that I use on a little html parser that I
wrote.  You may be able to adjust it for your needs:

class Characters:
    def __init__(self, String):
        self.Characters=String
        self.Char = ""
        self.index = 0
        self.lenght = len(self.Characters)

    def GetNextChar(self):
        skipchar = 1
        while skipchar ==1:
            try:
                self.Char = self.Characters[self.index]
            except IndexError:
                self.Char = None
                #print "End of File\n"
                return None
            self.index += 1
            if self.Char != "\n":
                skipchar = 0
        return self.Char

    def CheckNextChar(self):
        skipchar = 1
        StartChar = self.Char
        StartIndex = self.index
        while skipchar ==1:
            try:
                self.Char = self.Characters[self.index]
            except IndexError:
                self.Char = None
                #print "End of File\n"
                return None
            self.index += 1
            if self.Char != "\n":
                skipchar = 0
        self.index = StartIndex
        NextChar = self.Char
        self.Char = StartChar
        return NextChar




More information about the Python-list mailing list