2

我对 Python 很陌生,正在尝试编写一个用于 ArcGIS 10.1 (arcpy) 的脚本;基本思想是添加一个新字段(francis),检查其他几个字段中的值,如果有的话为空(-99)然后输出 0 到 Francis,否则运行一个简单的计算。但是,我得到了这个错误和我无法超越它:

回溯(最近一次通话最后):

文件“C:\gislab2\Python\take_home\part1\prelim_if2.py”,第 28 行,在 arcpy.CalculateField_management(Output_Feature_Class, "Francis", "", "PYTHON_9.3", "")

文件“C:\Program Files\ArcGIS\Desktop10.1\arcpy\arcpy\management.py”,第 3128 行,在 CalculateField raise e

ExecuteError:执行失败。参数无效。错误 000735:表达式:需要值 执行失败 (CalculateField)。

这是代码

# Import arcpy module
import arcpy

print "start your engines"
# Script arguments
Shapefile = "C:\\gislab2\\Python\\take_home\\USCancer2000.shp"

Field_Name = Francis

Output_Feature_Class = "C:\\gislab2\\Python\\take_home\\USCancer2000.shp"

# Local variables:
USCancer2000__2_ = Output_Feature_Class

# Process: Add Field
arcpy.AddField_management(Shapefile, "Francis", "LONG", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")

# Process: Calculate Field
arcpy.CalculateField_management(Output_Feature_Class, "Francis", "", "PYTHON_9.3", "")
##
### Process: If-then check for missing values
##
if "Cnt1"==-99:
    Field_name=7
elif "Cnt2"==-99:
    Field_name=7
elif "Cnt3"==-99:
    Field_name=7
elif "Pop1"==-99:
    Field_name==7
elif "Pop2"==-99:
    Field_name=7
elif "Pop3"==-99:
    Field_name=7
else:
        Field_name=("Cnt1"+"Cnt2"+"Cnt3")/("Pop1"+"Pop2"+"Pop3")
print "done"

提前谢谢了!大卫

4

1 回答 1

3

第三个参数arcpy.CalculateField_management告诉它要计算什么。你没有在那里传递任何东西。作为测试,将该行替换为 arcpy.CalculateField_management(Output_Feature_Class, "Francis", 5, "PYTHON_9.3", "")并查看它是否评估。

成功运行后,您应该考虑使用表达式和代码块来进行所需的计算。请参阅此处的计算范围(第三个)示例。

- 选择 -

您还可能会发现使用updateCursor更容易。这似乎比使用 calculateField 需要更多的工作,但它可以更快,并且省去了编写复杂代码块的麻烦。

# Import arcpy module
import arcpy

print "start your engines"
# Script arguments
Shapefile = "C:\\gislab2\\Python\\take_home\\USCancer2000.shp"

Field_Name = "Francis"

# Process: Add Field
arcpy.AddField_management(Shapefile, "Francis", "LONG", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")

fields = ["Cnt1", "Cnt2", "Cnt3", "Pop1", "Pop2", "Pop3", "Francis"] # the fields you want available to you in the cursor

with arcpy.da.UpdateCursor(shapefile, fields) as cursor:
    for row in cursor: # step through each row
        if not -99 in row: # Check for nulls
            # none found, do the math
            row[6] = (row[0] + row[1] + row[2]) / (row[3] + row[4] + row[5])
        else:
            # nulls found, zero out the result
            row[6] = 0
        cursor.updateRow(row) # save it

print "done"
于 2013-04-05T17:57:40.423 回答