嘗試在ArcGIS 10.1中通過UpdateCursor執行一些看似簡單的字段計算,並獲取有關無法迭代浮動的錯誤。這是我的代碼 - 有些東西被註釋掉了,它對我的問題並不重要,所以只是忽略它。arcpy TypeError:'float'對象不可迭代
#import arcpy module
import arcpy
#doing some fancy math
import math
#message to let you know the script started
print "Begin Field Calculation for age-adjusted-rate."
#input shapefile
inputFC = 'C:\\blahblah.shp'
#variable to define the new field name
Field_Name = ['Age_Adj_R', 'wt_1', 'wt_2', 'wt_3']
#add the new Fields
#arcpy.AddField_management(inputFC, Field_Name[0], "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
#arcpy.AddField_management(inputFC, Field_Name[1], "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
#arcpy.AddField_management(inputFC, Field_Name[2], "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
#arcpy.AddField_management(inputFC, Field_Name[3], "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
#list variable for the fields in the table that will be used
fields = ["Cnt1", "Cnt2", "Cnt3", "Pop1", "Pop2", "Pop3", "Crude_Rate", "Age_Adj_R", "wt_1", "wt_2", "wt_3"]
#wt_age_avg = [0.2869, 0.5479, 0.1652]
#populate the weighted average fields
cursor = arcpy.da.InsertCursor(inputFC, ["wt_1", "wt_2", "wt_3"])
for x in xrange(0, 51):
cursor.insertRow([0.2869, 0.5479, 0.1652])
del cursor
#function to perform the field calculation using an update cursor
with arcpy.da.UpdateCursor(inputFC, fields) as cursor:
for row in cursor: #iterate through each row
if not -99 in row: #check for missing values
row[7] = str(sum((row[6]) * ((row[0] * row[8]) + (row[1] * row[9]) + (row[2] * row[10]))) #do the calculation
else:
row[7] = 0 #missing values found, place a null response
cursor.updateRow(row) #save the calculations
del row #release the variables
#acknowledge completion
print "Calculation Completed."
錯誤在IDLE:
Traceback (most recent call last):
File "C:\blahblah.py", line 48, in <module>
row[7] = str(sum((row[6]) * ((row[0] * row[8]) + (row[1] * row[9]) + (row[2] * row[10])))) #do the calculation
TypeError: 'float' object is not iterable
好吧 - 但我想我把它改成一個字符串之前,它甚至會填充字段....我不知道如何得到這個計算上班。它應該是這個樣子:
總和(crude_rate *總和(weighted_averages))
如果我使用常量的值字段不工作的方式,我也試圖傳遞值作爲變量(見變量:wt_age_avg )沒有運氣。另外使用其他求和功能,如math.fsum也沒有工作。
'打印(行)'它打印出來的清單? – User 2013-04-08 18:19:54