2012-02-17 78 views
0

我正在使用python編寫代碼以在ArcMAP中生成一個點shapefile。我有1000個隨機可能性(在FileA中),我需要嘗試所有這些可能性。 FileA是FileB位置的索引。AttributeError:'str'對象沒有屬性'toInteger'

此外,對於每個生成的序列,我正在查看Voronoi多邊形的面積從3個點到50個點的演變,每個序列中包含總共50個點。

當我運行的代碼,我有這個消息,這讓我困惑:

tempXYFile.writerow('{0},{1}'.format(coordinates[entry.toInteger()][0],coordinates[entry.toInteger()][1])) 
AttributeError: 'str' object has no attribute 'toInteger' 

我會感謝任何幫助!

這是我的代碼:

import csv 

# create 1000 XY sequences 
print 'Start of the script' 
sequences = csv.reader(open('FileA.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('FileB.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','wb'),delimiter=',') #add the parameter to create a file if does not exist     
    for entry in sequence:   
     tempXYFile.writerow('{0},{1}'.format(coordinates[entry.toInteger()][0],coordinates[entry.toInteger()][1])) 
     print 'Entry # {0}: {1},{2}'.format(j, coordinates[entry.toInteger()][0],coordinates[entry.toInteger()][1]) 
     j = j + 1 
    i = i + 1 
    del tempXYFile 

print 'End of Script' 
+0

嗨,改變了 'entry.toInteger()[0]' 至「INT(條目[])'。混合語言... 我有這個新錯誤: 'tempXYFile.writerow('{0},{1}'.format(coordinates [int(entry)] [0],coordinates [int(entry)] [ 1])) IndexError:列表索引超出範圍' – user1166251 2012-02-17 22:55:28

回答

2

Python沒有toInteger()函數用於字符串,您將希望用戶int(entry[])代替。

2

有在標準Python字符串沒有toInteger()方法。錯誤消息非常清楚。如果要將像"37"這樣的字符串轉換爲整數37,請嘗試使用int(entry)

爲什麼你在你的代碼中寫入toInteger()