How do you properly format a code block on a CalculateField in a python toollbox?
I receive an error similar to this:
Traceback (most recent call last):
File "<string>", line 128, in execute
File "c:\program files (x86)\arcgis\desktop10.8\arcpy\arcpy\management.py", line 3647, in CalculateField
raise e
ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000989: Python syntax error: Parsing error IndentationError: unexpected indent (line 2)
Failed to execute (CalculateField).
Solved! Go to Solution.
The following code is from one of the examples on the Calculate Field tool help page. Does that help?
expression = "getClass(float(!SHAPE.area!))"
codeblock = """
def getClass(area):
if area <= 1000:
return 1
if area > 1000 and area <= 10000:
return 2
else:
return 3"""
# Execute CalculateField
arcpy.CalculateField_management(inTable, fieldName, expression, "PYTHON3", codeblock)
The following code is from one of the examples on the Calculate Field tool help page. Does that help?
expression = "getClass(float(!SHAPE.area!))"
codeblock = """
def getClass(area):
if area <= 1000:
return 1
if area > 1000 and area <= 10000:
return 2
else:
return 3"""
# Execute CalculateField
arcpy.CalculateField_management(inTable, fieldName, expression, "PYTHON3", codeblock)