开发者

problem with field calculation in a python script of a shp file

I added a field and wanted to calculate field by codeblock but the code block is not working, I guess. In the output shp file, all the value is showing 0. Here is the code:

# 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:
retur开发者_如何学Gon 1
if length > 600.0 and length <= 6000.0:
return 2
else:
return 3
"""
gp.CalculateField_management(inputFC, "lenclass", expression, "PYTHON", codeblock)`


The indents in a code block are 2 space indents withing the calculate field tool's codeblock (arcgis 10).

def getclass(length):
  if length <= 600.0:
    return 1
  if length > 600.0 and length <= 6000.0:
    return 2
  else:
    return 3


Python uses whitespace to delineate code blocks, and your string has none. Try this:

codeblock = """\
def getclass(length):
    if length <= 600.0:
        return 1
    if length > 600.0 and length <= 6000.0:
        return 2
    else:
        return 3
"""


Triple-quoted strings in Python automatically continue on to subsequent lines until the matching end quotes, but they also only include whatever whitespace you give them. You may have some indentation issues for your code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜