尝试通过 ArcGIS 10.1 中的 UpdateCursor 进行一些看似简单的字段计算,并收到有关无法迭代浮点数的错误。这是我的代码——有些东西被注释掉了 b/c 这对我的问题并不重要,所以忽略它。
#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
好的——但我想我在它甚至会填充字段之前将它更改为一个字符串......我不知道如何让这个计算工作。它应该看起来像:
sum(crude_rate* sum(weighted_averages))
如果我使用常量值字段的方式不起作用,我也尝试将值作为变量传递(请参阅变量:wt_age_avg),但没有运气。也使用其他求和函数,如 math.fsum 也不起作用。