2

I have an attribute table with over 2500 fields. About 2000 fields start with the name "All_txt_" (eg "All_txt_Watermelon").

Is there a way to remove this "All_txt_" from all the fieldnames automatically? (so the name would be "Watermelon"). Or does anyone know how to do so in Python? I work with the 10.1 version and the Alter Field tool is not an option.

  • This Question covers your options, either create new fields, use QGIS, or convert to gdb feature class. Python can be used to automate certain aspects of all of these methods. – landocalrissian Apr 24 '15 at 11:45
  • Actually it doesn't. It tells the automated solution for arcgis 10.2 or how to change fields manually in QGIS/ArcGIS 10.1. However, with over 2500 fields i need a script to automatize the process. bzt I appreciate your input though, thanks! – user3476078 Apr 27 '15 at 3:07
7

My method for this was to use the Field Mappings and Table to Table conversion functions. Using field mappings you can set new fields based on the names of fields in your input table.

The following link has examples for feature classes: http://resources.arcgis.com/en/help/main/10.1/index.html#//018z0000007p000000

My code below will change all fields in an input table that start with "All_txt_" to a new name. For example "All_txt_SomeName" to "SomeName".

import os
import arcpy

table = r"PATH TO INPUT TABLE"
new_tb = r"PATH TO OUTPUT TABLE"

field_mappings = arcpy.FieldMappings() # Create new field mapping object

#Loop through fields. For each field, create a corresponding FieldMap object.
#If the name begins with "ALL_txt_", create a new name.
#Note: "FID and "Shape" are skipped

for field in arcpy.ListFields(table):
    if not field.name == "FID" and not field.name == "Shape":
        old_name = field.name

        #Rename if necessary
        if old_name.startswith("All_txt_"):
            new_name = old_name.strip("All_txt_")
        else:
            new_name = old_name

        #Create new FieldMap object    
        new_f = arcpy.FieldMap()
        new_f.addInputField(table,old_name) # Specify the input field to use

        #Rename output field
        new_f_name = new_f.outputField
        new_f_name.name = new_name
        new_f_name.aliasName = new_name
        new_f.outputField = new_f_name

        #Add field to FieldMappings object
        field_mappings.addFieldMap(new_f)

#Convert table using your created Field Mappings object
arcpy.TableToTable_conversion(table, os.path.dirname(new_tb), os.path.basename(new_tb), field_mapping=field_mappings)

Hope this solves your problem!

(If you wish to carry out this process on a feature class, just specify the input/output as valid feature class paths and use FeatureClassToFeatureClass_conversion.)

  • Hi, thank you a lot! This is exactly what I need! However when I run the script I get these errors: Runtime error Traceback File "<string>", line 1, in <module> File "c:\program files (x86)\arcgis\desktop10.1\arcpy\arcpy_init_.py", line 1075, in ListFields return gp.listFields(dataset, wild_card, field_type) File "c:\program files (x86)\arcgis\desktop10.1\arcpy\arcpy\geoprocessing_base.py", line 344, in listFields self._gp.ListFields(*gp_fixargs(args, True))) RuntimeError: ERROR 999999: Error executing function. Any idea on how to handle these or why they appear? – user3476078 Apr 27 '15 at 6:41
  • Frustratingly, the ERROR 9999999 is a generic error and could be a number of reasons. Several questions - was the input path a DBF file? Did you run the script outside of Arc? I didn't have any issues, so i'm hoping it's just a quick fix. – Ali Apr 27 '15 at 8:06
  • I think it may be an issue importing arcpy (assuming you ran the script outside of ArcMap). Try saving the above script, including your file paths, to a ".py" file. Then open the python console in ArcMap, right-click the console window, click "Load". Load the python file then press return to run. Let me know how you get on. – Ali Apr 27 '15 at 8:40
  • Thank you, it worked perfectly fine now! Indeed it was an issue with running it outside of ArcMap! Thanks also for the detailed description so a non-python person like me can understand it! – user3476078 Apr 28 '15 at 9:49
  • Great news! Glad to help. – Ali Apr 28 '15 at 10:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.