- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我对 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/
我在 ArcGIS 中使用 Arcpy 并尝试编写代码来更新上个月最后一天的字段(即,如果我今天运行代码,2017 年 9 月 1 日,它将更新该字段与 2017-08-31). 如果我进行手动字段计
我对 Python 很陌生,正在尝试编写一个用于 ArcGIS 10.1 (arcpy) 的脚本;基本思想是添加一个新字段(francis),检查其他几个字段中的值,如果有的话为空(-99)然后输出
我是一名优秀的程序员,十分优秀!