<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Alter a field and Calculating a field issues in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/alter-a-field-and-calculating-a-field-issues/m-p/1513929#M71192</link>
    <description>&lt;P&gt;Hello, I am trying to either:&lt;/P&gt;&lt;P&gt;a) Alter a field name and looping through several tables to change many (similar) names to the same name, or&lt;/P&gt;&lt;P&gt;b) calculate a field. Both seem very simple, but I am not understanding the errors being thrown, or why they are not working.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Example a) Alter a field. Code runs, but does not actually alter the field name&lt;/P&gt;&lt;P&gt;Code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;import acrpy&lt;/P&gt;&lt;P&gt;# Set workspace&lt;BR /&gt;arcpy.env.workspace = r'G:\G_and_E_spp\E_Spp\e_combine_tables.gdb'&lt;/P&gt;&lt;P&gt;# Loop through tables looking for a field named '*_CON'&lt;BR /&gt;fcList = arcpy.ListFeatureClasses() # Get a list of feature classes&lt;BR /&gt;for fc in fcList: # Loop through feature classes&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; fieldList = arcpy.ListFields(fc) # Get a list of fields for each feature class&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; for field in fieldList: # Lloop through each field&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if field.name.lower() =='aADSAx_CON':&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; arcpy.management.AlterField(fc, field.name, 'aADSAx_CON', 'sp_con', 'species condition')&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;b) Calculate field&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Code:&lt;/P&gt;&lt;P&gt;import arcpy&lt;BR /&gt;inTab = r"G:\G_and_E_spp\E_Spp\combine_tables_dbf\aADSAx_CONUS_0123_2001v1_2012.dbf"&lt;BR /&gt;expr_1 = !sp_con! = !aADSAx_CON!&lt;BR /&gt;arcpy.CalculateField_management(inTab,"aADSAx_CON",expr_1,"PYTHON3")&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Error:&lt;/P&gt;&lt;PRE&gt;&lt;SPAN class=""&gt;RuntimeError&lt;/SPAN&gt;                              Traceback (most recent call last)
In  &lt;SPAN class=""&gt;[69]&lt;/SPAN&gt;:
Line &lt;SPAN class=""&gt;4&lt;/SPAN&gt;:     arcpy.CalculateField_management(inTab,&lt;SPAN class=""&gt;"&lt;/SPAN&gt;&lt;SPAN class=""&gt;aADSAx_CON&lt;/SPAN&gt;&lt;SPAN class=""&gt;"&lt;/SPAN&gt;,expr_1,&lt;SPAN class=""&gt;"&lt;/SPAN&gt;&lt;SPAN class=""&gt;PYTHON3&lt;/SPAN&gt;&lt;SPAN class=""&gt;"&lt;/SPAN&gt;)

File &lt;SPAN class=""&gt;C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\management.py&lt;/SPAN&gt;, in &lt;SPAN class=""&gt;CalculateField&lt;/SPAN&gt;:
Line &lt;SPAN class=""&gt;5711&lt;/SPAN&gt;:  &lt;SPAN class=""&gt;raise&lt;/SPAN&gt; e

File &lt;SPAN class=""&gt;C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\management.py&lt;/SPAN&gt;, in &lt;SPAN class=""&gt;CalculateField&lt;/SPAN&gt;:
Line &lt;SPAN class=""&gt;5708&lt;/SPAN&gt;:  retval = convertArcObjectToPythonObject(gp.CalculateField_management(*gp_fixargs((in_table, field, expression, expression_type, code_block, field_type, enforce_domains), &lt;SPAN class=""&gt;True&lt;/SPAN&gt;)))

File &lt;SPAN class=""&gt;C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\geoprocessing\_base.py&lt;/SPAN&gt;, in &lt;SPAN class=""&gt;&amp;lt;lambda&amp;gt;&lt;/SPAN&gt;:
Line &lt;SPAN class=""&gt;512&lt;/SPAN&gt;:   &lt;SPAN class=""&gt;return&lt;/SPAN&gt; &lt;SPAN class=""&gt;lambda&lt;/SPAN&gt; *args: val(*gp_fixargs(args, &lt;SPAN class=""&gt;True&lt;/SPAN&gt;))

&lt;SPAN class=""&gt;RuntimeError&lt;/SPAN&gt;: Object: Error in executing tool&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Any help very much appreciated!!&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 01 Aug 2024 22:33:32 GMT</pubDate>
    <dc:creator>el_D</dc:creator>
    <dc:date>2024-08-01T22:33:32Z</dc:date>
    <item>
      <title>Alter a field and Calculating a field issues</title>
      <link>https://community.esri.com/t5/python-questions/alter-a-field-and-calculating-a-field-issues/m-p/1513929#M71192</link>
      <description>&lt;P&gt;Hello, I am trying to either:&lt;/P&gt;&lt;P&gt;a) Alter a field name and looping through several tables to change many (similar) names to the same name, or&lt;/P&gt;&lt;P&gt;b) calculate a field. Both seem very simple, but I am not understanding the errors being thrown, or why they are not working.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Example a) Alter a field. Code runs, but does not actually alter the field name&lt;/P&gt;&lt;P&gt;Code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;import acrpy&lt;/P&gt;&lt;P&gt;# Set workspace&lt;BR /&gt;arcpy.env.workspace = r'G:\G_and_E_spp\E_Spp\e_combine_tables.gdb'&lt;/P&gt;&lt;P&gt;# Loop through tables looking for a field named '*_CON'&lt;BR /&gt;fcList = arcpy.ListFeatureClasses() # Get a list of feature classes&lt;BR /&gt;for fc in fcList: # Loop through feature classes&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; fieldList = arcpy.ListFields(fc) # Get a list of fields for each feature class&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; for field in fieldList: # Lloop through each field&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if field.name.lower() =='aADSAx_CON':&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; arcpy.management.AlterField(fc, field.name, 'aADSAx_CON', 'sp_con', 'species condition')&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;b) Calculate field&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Code:&lt;/P&gt;&lt;P&gt;import arcpy&lt;BR /&gt;inTab = r"G:\G_and_E_spp\E_Spp\combine_tables_dbf\aADSAx_CONUS_0123_2001v1_2012.dbf"&lt;BR /&gt;expr_1 = !sp_con! = !aADSAx_CON!&lt;BR /&gt;arcpy.CalculateField_management(inTab,"aADSAx_CON",expr_1,"PYTHON3")&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Error:&lt;/P&gt;&lt;PRE&gt;&lt;SPAN class=""&gt;RuntimeError&lt;/SPAN&gt;                              Traceback (most recent call last)
In  &lt;SPAN class=""&gt;[69]&lt;/SPAN&gt;:
Line &lt;SPAN class=""&gt;4&lt;/SPAN&gt;:     arcpy.CalculateField_management(inTab,&lt;SPAN class=""&gt;"&lt;/SPAN&gt;&lt;SPAN class=""&gt;aADSAx_CON&lt;/SPAN&gt;&lt;SPAN class=""&gt;"&lt;/SPAN&gt;,expr_1,&lt;SPAN class=""&gt;"&lt;/SPAN&gt;&lt;SPAN class=""&gt;PYTHON3&lt;/SPAN&gt;&lt;SPAN class=""&gt;"&lt;/SPAN&gt;)

File &lt;SPAN class=""&gt;C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\management.py&lt;/SPAN&gt;, in &lt;SPAN class=""&gt;CalculateField&lt;/SPAN&gt;:
Line &lt;SPAN class=""&gt;5711&lt;/SPAN&gt;:  &lt;SPAN class=""&gt;raise&lt;/SPAN&gt; e

File &lt;SPAN class=""&gt;C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\management.py&lt;/SPAN&gt;, in &lt;SPAN class=""&gt;CalculateField&lt;/SPAN&gt;:
Line &lt;SPAN class=""&gt;5708&lt;/SPAN&gt;:  retval = convertArcObjectToPythonObject(gp.CalculateField_management(*gp_fixargs((in_table, field, expression, expression_type, code_block, field_type, enforce_domains), &lt;SPAN class=""&gt;True&lt;/SPAN&gt;)))

File &lt;SPAN class=""&gt;C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\geoprocessing\_base.py&lt;/SPAN&gt;, in &lt;SPAN class=""&gt;&amp;lt;lambda&amp;gt;&lt;/SPAN&gt;:
Line &lt;SPAN class=""&gt;512&lt;/SPAN&gt;:   &lt;SPAN class=""&gt;return&lt;/SPAN&gt; &lt;SPAN class=""&gt;lambda&lt;/SPAN&gt; *args: val(*gp_fixargs(args, &lt;SPAN class=""&gt;True&lt;/SPAN&gt;))

&lt;SPAN class=""&gt;RuntimeError&lt;/SPAN&gt;: Object: Error in executing tool&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Any help very much appreciated!!&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 01 Aug 2024 22:33:32 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/alter-a-field-and-calculating-a-field-issues/m-p/1513929#M71192</guid>
      <dc:creator>el_D</dc:creator>
      <dc:date>2024-08-01T22:33:32Z</dc:date>
    </item>
    <item>
      <title>Re: Alter a field and Calculating a field issues</title>
      <link>https://community.esri.com/t5/python-questions/alter-a-field-and-calculating-a-field-issues/m-p/1513982#M71194</link>
      <description>&lt;LI-CODE lang="python"&gt;if field.name.lower() =='aADSAx_CON':
    arcpy.management.AlterField(fc, field.name, 'aADSAx_CON', 'sp_con', 'species condition')&lt;/LI-CODE&gt;&lt;P&gt;isn't field.name redundant in line 2, if you want to rename 'aADSAx_con` to `sp_con` ?&lt;/P&gt;</description>
      <pubDate>Fri, 02 Aug 2024 00:52:01 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/alter-a-field-and-calculating-a-field-issues/m-p/1513982#M71194</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2024-08-02T00:52:01Z</dc:date>
    </item>
    <item>
      <title>Re: Alter a field and Calculating a field issues</title>
      <link>https://community.esri.com/t5/python-questions/alter-a-field-and-calculating-a-field-issues/m-p/1514020#M71195</link>
      <description>&lt;P&gt;Hi Dan,&amp;nbsp;&lt;/P&gt;&lt;P&gt;Yes, Thank you for&amp;nbsp; responding. The issue was that there is essentially a wild card for each xWXYZx_CON name in thousands of tables.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I was able to resolve it using&amp;nbsp;&lt;/P&gt;&lt;P&gt;import arcpy&lt;/P&gt;&lt;P&gt;# Set workspace&lt;BR /&gt;arcpy.env.workspace = r'G:\G_and_E_spp\E_Spp\e_combine_tables.gdb'&lt;/P&gt;&lt;P&gt;# List of old and new field names&lt;BR /&gt;field_name_mappings = [&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;('xWXYZx', 'sp_con', 'species condition'),&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;#add entire list of field names....&lt;/P&gt;&lt;P&gt;}&lt;/P&gt;&lt;P&gt;# Loop through feature classes&lt;BR /&gt;Tables = arcpy.ListTables()&lt;BR /&gt;if not Tables:&lt;BR /&gt;print("No feature classes found in workspace.")&lt;BR /&gt;else:&lt;BR /&gt;print(f"Found {len(fcList)} feature classes.")&lt;/P&gt;&lt;P&gt;for tb in Tables:&lt;BR /&gt;print(f"\nProcessing feature class: {fc}")&lt;BR /&gt;&lt;BR /&gt;# List fields in the feature class&lt;BR /&gt;fieldList = arcpy.ListFields(fc)&lt;BR /&gt;if not fieldList:&lt;BR /&gt;print(f"No fields found in feature class: {fc}")&lt;BR /&gt;continue&lt;BR /&gt;&lt;BR /&gt;field_names = [field.name for field in fieldList]&lt;BR /&gt;print(f"Existing fields: {field_names}")&lt;BR /&gt;&lt;BR /&gt;# Iterate over the field name mappings&lt;BR /&gt;for old_name, new_name, alias in field_name_mappings:&lt;BR /&gt;if old_name in field_names:&lt;BR /&gt;try:&lt;BR /&gt;print(f"Renaming field '{old_name}' to '{new_name}' in {fc}")&lt;BR /&gt;arcpy.management.AlterField(fc, old_name, new_name, alias)&lt;BR /&gt;print(f"Field '{old_name}' successfully renamed to '{new_name}'")&lt;BR /&gt;except Exception as e:&lt;BR /&gt;print(f"Error renaming field '{old_name}' in {fc}: {e}")&lt;BR /&gt;else:&lt;BR /&gt;print(f"Field '{old_name}' not found in {fc}")&lt;/P&gt;</description>
      <pubDate>Fri, 02 Aug 2024 05:23:52 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/alter-a-field-and-calculating-a-field-issues/m-p/1514020#M71195</guid>
      <dc:creator>el_D</dc:creator>
      <dc:date>2024-08-02T05:23:52Z</dc:date>
    </item>
  </channel>
</rss>

