python - Index out of Range in a 'for' loop -
i working on project converting csv file arcgis shapefile, involves writing output separate file. have created list each column in each row , trying index column 36 , 37. however, getting list index out of range
error message when doing this. suggestions may doing?
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 item in newlist[36]: item.replace("", "0") item in newlist[37]: item.replace("", "0") newline = ','.join(newlist) newline = newline + '\n' formatline = newline.replace("/","_") outfile.write(formatline)
it if edit question include line error says having index out of range problem.
i believe issue this:
while line: # line other whitespace line = infile.readline() # next line becomes whitespace, there might trailing newline character in file ... newlist = line.split(',')[:72] # if line.split(',') doesn't return list @ least 72 values, there not error here- merely return shorter list. item in newlist[36]: # newlist empty list @ point. ...
on side note, typed following python shell:
>>> bool("") false >>> bool(" ") true >>> bool("\n") true
as can see, if there line has space in it, loop would've continued well.
Comments
Post a Comment