2012-02-17 150 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)] 

,看看是不是你認爲它是。

相關問題