mikevann

Python Index Out of Range Error

Discussion created by mikevann on Apr 23, 2013
Latest reply on Apr 25, 2013 by curtvprice
Hi,

I am looping through a csv file using Python to remove header lines, unnecessary columns and trying to replace any blank latitude and longitude coordinate values with a value of (0, 0), these coordinates are in two separate columns. For this replace process I am using a ???for??? loop and the ???replace??? method and am indexing (in the case of the csv file I am working with, the index of the two columns are 36 and 37). However, I keep getting a ???list index out of range??? error message when doing this. Any thoughts to point me in a better direction?

Here's the code:
count = 0
for n in range(6):
    count = count + 1
    line = inFile.readline()

while line:
    count = count + 1
    line = inFile.readline()
    print 'Row', count, 'line info=', line[:72]
    lineList = line.split(',')
    newList = lineList[:72]
    print 'line info =', newList  
    for item in newList[36]: #-> use "if" statement; for column, if name == "LatitudeNAD83" and 
        item.replace("", "0")
    for item in newList[37]:
        item.replace("", "0")
    newLine = ','.join(newList)
    newLine = newLine + '\n'
    formatLine = newLine.replace("/","_")
    outFile.write(formatLine) 


Thanks!

UPDATE: Here's my code in correct format

newList is a list of field names, there should be 72 items in it.[/QUOTE]

Outcomes