Select to view content in your preferred language

Alter a field and Calculating a field issues

235
2
08-01-2024 03:33 PM
el_D
by
New Contributor

Hello, I am trying to either:

a) Alter a field name and looping through several tables to change many (similar) names to the same name, or

b) calculate a field. Both seem very simple, but I am not understanding the errors being thrown, or why they are not working.

 

Example a) Alter a field. Code runs, but does not actually alter the field name

Code:

 

import acrpy

# Set workspace
arcpy.env.workspace = r'G:\G_and_E_spp\E_Spp\e_combine_tables.gdb'

# Loop through tables looking for a field named '*_CON'
fcList = arcpy.ListFeatureClasses() # Get a list of feature classes
for fc in fcList: # Loop through feature classes
      fieldList = arcpy.ListFields(fc) # Get a list of fields for each feature class
      for field in fieldList: # Lloop through each field
         if field.name.lower() =='aADSAx_CON':
            arcpy.management.AlterField(fc, field.name, 'aADSAx_CON', 'sp_con', 'species condition')

 

b) Calculate field

 

Code:

import arcpy
inTab = r"G:\G_and_E_spp\E_Spp\combine_tables_dbf\aADSAx_CONUS_0123_2001v1_2012.dbf"
expr_1 = !sp_con! = !aADSAx_CON!
arcpy.CalculateField_management(inTab,"aADSAx_CON",expr_1,"PYTHON3")

 

Error:

RuntimeError                              Traceback (most recent call last)
In  [69]:
Line 4:     arcpy.CalculateField_management(inTab,"aADSAx_CON",expr_1,"PYTHON3")

File C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\management.py, in CalculateField:
Line 5711:  raise e

File C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\management.py, in CalculateField:
Line 5708:  retval = convertArcObjectToPythonObject(gp.CalculateField_management(*gp_fixargs((in_table, field, expression, expression_type, code_block, field_type, enforce_domains), True)))

File C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\geoprocessing\_base.py, in <lambda>:
Line 512:   return lambda *args: val(*gp_fixargs(args, True))

RuntimeError: Object: Error in executing tool


Any help very much appreciated!!

 

 

0 Kudos
2 Replies
DanPatterson
MVP Esteemed Contributor
if field.name.lower() =='aADSAx_CON':
    arcpy.management.AlterField(fc, field.name, 'aADSAx_CON', 'sp_con', 'species condition')

isn't field.name redundant in line 2, if you want to rename 'aADSAx_con` to `sp_con` ?


... sort of retired...
0 Kudos
el_D
by
New Contributor

Hi Dan, 

Yes, Thank you for  responding. The issue was that there is essentially a wild card for each xWXYZx_CON name in thousands of tables. 

I was able to resolve it using 

import arcpy

# Set workspace
arcpy.env.workspace = r'G:\G_and_E_spp\E_Spp\e_combine_tables.gdb'

# List of old and new field names
field_name_mappings = [


('xWXYZx', 'sp_con', 'species condition'),

 

#add entire list of field names....

}

# Loop through feature classes
Tables = arcpy.ListTables()
if not Tables:
print("No feature classes found in workspace.")
else:
print(f"Found {len(fcList)} feature classes.")

for tb in Tables:
print(f"\nProcessing feature class: {fc}")

# List fields in the feature class
fieldList = arcpy.ListFields(fc)
if not fieldList:
print(f"No fields found in feature class: {fc}")
continue

field_names = [field.name for field in fieldList]
print(f"Existing fields: {field_names}")

# Iterate over the field name mappings
for old_name, new_name, alias in field_name_mappings:
if old_name in field_names:
try:
print(f"Renaming field '{old_name}' to '{new_name}' in {fc}")
arcpy.management.AlterField(fc, old_name, new_name, alias)
print(f"Field '{old_name}' successfully renamed to '{new_name}'")
except Exception as e:
print(f"Error renaming field '{old_name}' in {fc}: {e}")
else:
print(f"Field '{old_name}' not found in {fc}")

0 Kudos