I'm trying to calculate a field from another field using arcpy. If the field contains the word Diversion, I want it to calculate the other field with the value 3. If it conctains 'Dam' I want it to calculate 9, otherwise keep it null.
When I try to execute this in the python window, itlooks like it's working, but my field is still null. The new field is a long integer. Please Help!
Category = "StructureCategory"
expression5 = "Reclass(!StructureT!)"
codeblock = """def Reclass (newdams_schema):
if 'Diversion' in newdams_schema:
return 3
elif 'Dam' in newdams_schema:
return 9
else:
return None"""
# Execute CalculateField
arcpy.CalculateField_management(newdams_schema, Category, expression5, "PYTHON_9.3")
Solved! Go to Solution.
are you doing this in arcmap or arcgis pro? (because PYTHON3 is used in Pro)
I think you missed codeblock.
Also, you don't need to return None and you can format your codeblock as
Category = "StructureCategory"
expression5 = "Reclass(!StructureT!)"
codeblock = """
def Reclass (newdams_schema):
if 'Diversion' in newdams_schema:
return 3
elif 'Dam' in newdams_schema:
return 9
else:
return
"""
arcpy.management.CalculateField(newdams_schema,
field=Category,
expression=expression5,
expression_type="PYTHON",
code_block=codeblock)
are you doing this in arcmap or arcgis pro? (because PYTHON3 is used in Pro)
I think you missed codeblock.
Also, you don't need to return None and you can format your codeblock as
Category = "StructureCategory"
expression5 = "Reclass(!StructureT!)"
codeblock = """
def Reclass (newdams_schema):
if 'Diversion' in newdams_schema:
return 3
elif 'Dam' in newdams_schema:
return 9
else:
return
"""
arcpy.management.CalculateField(newdams_schema,
field=Category,
expression=expression5,
expression_type="PYTHON",
code_block=codeblock)
Thank you!