Code Block in Python Toolbox

297
1
Jump to solution
09-01-2021 10:49 AM
Labels (1)
MichaelStranovsky
Occasional Contributor

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).

 

0 Kudos
1 Solution

Accepted Solutions
Tim_McGinnes
Occasional Contributor III

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)

 

View solution in original post

1 Reply
Tim_McGinnes
Occasional Contributor III

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)