我正在研究将csv文件转换为ArcGIS shapefile的项目,这涉及将输出写入单独的文件。我已经为每一行中的每一列创建了一个列表,并尝试索引列36和37.但是,我在执行此操作时收到list index out of range
错误消息。关于我可能在做什么的任何建议?
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]:
item.replace("", "0")
for item in newList[37]:
item.replace("", "0")
newLine = ','.join(newList)
newLine = newLine + '\n'
formatLine = newLine.replace("/","_")
outFile.write(formatLine)
答案 0 :(得分:3)
如果您可以编辑问题以包含错误所说的索引超出范围问题,那将会有所帮助。
我认为这个问题可能就是这个问题:
while line: # line is something other than whitespace
line = inFile.readline() # next line becomes whitespace, there might be a trailing newline character in the file
...
newList = line.split(',')[:72] # Even if line.split(',') doesn't return a list with at least 72 values, there will not be an error here- it will merely return a shorter list.
for item in newList[36]: # newList is probably an empty list at this point.
...
另外,我在Python shell中输入以下内容:
>>> bool("")
False
>>> bool(" ")
True
>>> bool("\n")
True
如您所见,如果有一条线只有一个空格,那么循环也会继续。