Issue with CalculateField function

264
2
09-13-2022 01:15 PM
Montgomerycd
New Contributor

Hi there I am having an issue with a snippet of code. my company created a tool and I am tasked with updating it to 3.0+ from 2.7

here is the snippet. I think the issue lies somewhere within Line 5, but for the life of me cant figure it out.

 

 

try:
arcpy.AddMessage("Adding new fields")
arcpy.management.AddField(rf"{prefix}\{user}\UnionData_temp.dbf" "Landuse", "TEXT")
arcpy.AddMessage("Fields Added")
arcpy.management.CalculateField(rf"{prefix}\{user}\UnionData_temp.dbf" "LandUse", "!LandUseCod! + " " + !LandUseC_1! + " " + !LandUseC_2!", "PYTHON3")
arcpy.AddMessage("Field Addition successful")
except Exception as e:
arcpy.AddMessage("Field addition failed")
arcpy.AddMessage(f"{e}")

 

 

 

Here is the error message that I am receiving when I run it the whole program as a tool in ArcGIS Pro

Adding new fields
Fields Added
Field addition failed
ERROR 000313: The length of Field Name must not be larger than 10
ERROR 000539: Traceback (most recent call last):
  File "<expression>", line 1, in <module>
NameError: name 'PYTHON3' is not defined

Failed to execute (CalculateField).

 

0 Kudos
2 Replies
DanPatterson
MVP Esteemed Contributor

The new python syntax is ....

Calculate Field (Data Management)—ArcGIS Pro | Documentation

if you are not saving to a file geodatabase, then you will have field name issues, so check your destination


... sort of retired...
0 Kudos
by Anonymous User
Not applicable

You're missing a comma separating the table and field.

 

 

arcpy.management.CalculateField(rf"{prefix}\{user}\UnionData_temp.dbf" "LandUse", "!LandUseCod! + " " + !LandUseC_1! + " " + !LandUseC_2!", "PYTHON3")

 

 

should be:

 

 

arcpy.management.CalculateField(rf"{prefix}\{user}\UnionData_temp.dbf", "LandUse", "!LandUseCod! + " " + !LandUseC_1! + " " + !LandUseC_2!", "PYTHON3")

 

 

 to follow the parameters:

 

 

arcpy.management.CalculateField(in_table, field, expression, {expression_type}, {code_block}, {field_type}, {enforce_domains})

 

Edit to add its also missing in the addfield method:

arcpy.management.AddField(rf"{prefix}\{user}\UnionData_temp.dbf", "Landuse", "TEXT")