gpt4 book ai didi

python - arcpy.CalculateField 上的 "Value is Required"

转载 作者:行者123 更新时间:2023-12-04 05:03:28 24 4
gpt4 key购买 nike

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

Traceback (most recent call last):

File "C:\gislab2\Python\take_home\part1\prelim_if2.py", line 28, in arcpy.CalculateField_management(Output_Feature_Class, "Francis", "", "PYTHON_9.3", "")

File "C:\Program Files\ArcGIS\Desktop10.1\arcpy\arcpy\management.py", line 3128, in CalculateField raise e

ExecuteError: Failed to execute. Parameters are not valid. ERROR 000735: Expression: Value is required Failed to execute (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"
提前谢谢了!
大卫

最佳答案

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

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

--另类--

您还会发现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"

关于python - arcpy.CalculateField 上的 "Value is Required",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15823621/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com