我添加了一個字段,想通過codeblock來計算字段,但是我猜,代碼塊不工作。在輸出SHP文件,所有值顯示爲0。這裏是代碼:Python中在一個shp文件的python腳本中的字段計算問題
# An input polygon feature class
inputFC = "D:/Delete/NewLineFeature.shp"
gp.AddField_management(inputFC, "lenclass", "SHORT")
# Calculation is based on a custom getclass definition
expression = "getclass(float(!shape.length!))"
codeblock = """\
def getclass(length):
if length <= 600.0:
return 1
if length > 600.0 and length <= 6000.0:
return 2
else:
return 3
"""
gp.CalculateField_management(inputFC, "lenclass", expression, "PYTHON", codeblock)`
我認爲縮進是問題,可能需要在返回行前添加\ t \ t,並且在if/else語句前加\ t。看起來字符串將由其他腳本運行。 – Jim