2012-02-17 182 views
-3

我正在使用python编写代码以在ArcMAP中生成一个点shapefile。我有1000个随机可能性50个点(在FileA(1000:50)中,我需要尝试所有这些点)IndexError:列表索引超出范围'

每个点的坐标X = FileB(:,1)。每个点的坐标Y = FileB(:,2)。

生成一个序列,我正在FileA的第一行,并且FileA(1,1)中的数字对应于FileB中点1的新序列中的位置。

我想我在其中创建以下FILEA的每一行中的位置这些序列创建一个循环

我以前的帖子: AttributeError: 'str' object has no attribute 'toInteger'

我将'entry.toInteger()[0]'改为'int(entry [])'。混合语言......

我有这个新的错误:

'tempXYFile.writerow('{0},{1}'.format(coordinates[int(entry)][0],coordinates[int(‌​entry)][1])) IndexError: list index out of range' 

我会感谢任何帮助!

这是我的代码:

import csv 

# create 1000 XY sequences 
print 'Start of the script' 
sequences = csv.reader(open('50VolcanoPaleoOrder-11-01-2012.csv','rb'),delimiter=',') 
coordinates = [] 

# read coordinates of volcanos and store in memory 
print 'Start reading in the coordinates into the memory' 
coordinates_file = csv.reader(open('seq50.csv','rb'),delimiter=',') 

for coordinate in coordinates[1:]: 
    coordinates.append(coordinate) 
del coordinates_file 

i = 1 
for sequence in sequences: 
    print 'sequence # {0}'.format(i) 
    j = 1   
    tempXYFile = csv.writer(open('tempXY.csv','w+'),delimiter=',') #add the parameter to create a file if does not exist     
    for entry in sequence:   
     tempXYFile.writerow('{0},{1}'.format(coordinates[int(entry)][0],coordinates[int(entry)][1])) 
     print 'Entry # {0}: {1},{2}'.format(j, coordinates[int(entry)][0],coordinates[int(entry)][1]) 
     j = j + 1 
    i = i + 1 
    del tempXYFile 

print 'End of Script' 
+0

提供完整的回溯。 – 2012-02-17 23:40:21

+0

'Traceback(last recent call last): 文件“C:\ Users \ Nicolas \ Documents \ RESEARCH \ AVF Voronoi sequence \ Voronoi-learningprocess.py”,第38行,在 tempXYFile.writerow('{0}, {1}'.format(coordinates [int(entry)] [0],coordinates [int(entry)] [1])) IndexError:列表索引超出范围' – user1166251 2012-02-17 23:52:34

+0

不要把它作为注释,put它进入你的问题(格式正确)。 – 2012-02-18 00:56:42

回答

2

在Python错误消息并不像某些语言编译错误坚不可摧;你应该试着了解他们告诉你的。

IndexError: list index out of range 

是一个标志,你是,以及使用不存在的索引访问列表。像“a = [1,2];打印[79]”会给你这个信息。在这种情况下,如果问题是在该行

tempXYFile.writerow('{0},{1}'.format(coordinates[int(entry)][0],coordinates[int(‌​entry)][1])) 

然后可能性是很好的,要么坐标不具有INT(条目)个元素,或坐标[INT(条目)]没有按”没有第0或第1个元素。

所以在该行之前,尝试插入打印语句:

print int(entry) 
print coordinates 
print coordinates[int(entry)] 

,看看是不是你认为它是。

相关问题