[Tutor] Iterating through tokens from a split command

Vicki Stanfield vicki at stanfield.net
Fri Mar 19 11:08:21 EST 2004


I am getting an Index Error as I loop through some data from an input
file. I don't get the error until several iterations into the data. Here
is the
code followed by an example of the input. The index error comes on the
first if statement inside the while loop.

TIA,
--vicki

------------------------------------
import fileinput, time, binascii

class ConvertFile:

    #Open logfile with name selected
    inputfile = open('F:/TimeResolve.dat','r')
    outputfile = open('F:/ConvertedTR.dat','w+')

    new = ""
    EndOfData = 0
    try:
        line = inputfile.readline()
        if line =="":
            EndOfData=1
        tokens=line.split("|")
        i=0
        while not EndOfData:
            outputfile.write("counter is "+str(i)+".\n")
            if tokens[i] == '0x11':
                new = '0x02|'
                outputfile.write(new)
            elif tokens[i] == '0x03':
                outputfile.write(tokens[i]+"|")
            elif len(tokens[i])==4:
                new=binascii.hexlify(tokens[i][3])+"|"
                outputfile.write('0x'+new)
            elif len(tokens[i])==3:
                new = '0x30|'
                new2=binascii.hexlify(tokens[i][2])+"|"
                outputfile.write(new+"0x"+new2)
            else:
                print "Just blah.\n"
                new = "blah"
                outputfile.write(new)
            i=i+1
    except IOError:
        print "Got IO Error.\n"

    outputfile.flush()
    inputfile.close()
    outputfile.close()


if __name__  == "__main__":
    ConvertFile()

-----------------------------
And the data:

0x11|0x4|0x14|0x21|0x0|0x21|0x7a|0x3|0x11|0x4|0x14|0x20|0x0|0x20|0x7a|0x3|0x11|0x4|0x0|0x0|0x0|0x45|0x2b|0x3|0x11|0x4|0x14|0x10|0x0|0x10|0x7a|0x3|0x11|0x8|0x16|0x91|0x0|0x0|0x0|0x0|0x0|0x0|0xe9|0x3|0x11|0x8|0x16|0x90|0x41|0xa9|0xe9|0xed|0x80|0x12|0x96|0x3|0x11|0x8|0x16|0x93|0x41|0x29|0xfd|0x4|0xf8|0x2f|0xad|0x3|0x11|0x8|0x16|0x92|0x0|0x0|0x0|0x0|0x0|0x0|0xea|0x3|0x11|0x8|0x16|0x95|0x0|0x0|0x0|0x0|0x0|0x0|0xed|0x3|0x11|0x8|0x16|0x94|0x0|0x0|0x0|0x0|0x0|0x0|0xec|0x3|0x11|0x2e|0x16|0x10|0x44|0xfa|0x0|0x0|0x0|0x0|0x0|0x0|0x0|0x0|0x0|0x0|0x0|0x0|0x0|0x0|0x0|0x0|0x0|0x0|0x0|0x0|0x0|0x0|0x0|0x0|0x0|0x0|0x0|0x0|0x0|0x0|0x0|0x0|0x0|0x0|0x0|0x0|0x0|0x0|0x0|0x0|0x0|0x0|0xd6|0x3|0x11|0x2e|0x16|0x20|0x44|0xfa|0x0|0x0|0x0|0x0|0x0|0x0|0x0|0x0|0x0|0x0|0x0|0x0|0x0|0x0|0x0|0x0|0x0|0x0|0x0|0x0|0x0|0x0|0x0|0x0|0x0|0x0|0x0|0x0|0x0|0x0|0x0|0x0|0x0|0x0|0x0|0x0|0x0|0x0|0x0|0x0|0x0|0x0|0xe6|0x3|0x11|0x2e|0x16|0x30|0x44|0xfa|0x0|0x0|0x3d|0x92|0x1f|0x6d|0xc2|0xbf|0xce|0x2|0x39|0x92|0x3b|0x6f|0x3f|0x1|!
0x42|0xfd|0x45|0xd8|0x46|0xdf|0x47|0x2d|0x46|0xb1|0x46|0x2|0x44|0x22|0x40|0x7e|0x3c|0x7d|0x39|0xa2|0x38|0x5b|0x38|0x55|0x38|0xdc|0x51|0x3|

"A pessimist sees the difficulty in every opportunity; an optimist sees
the opportunity in every difficulty."
  --  Winston Churchill




More information about the Tutor mailing list