I have 350 tables in a geodatabase. Using a python script I want to iterate through all 350 tables and alter the field names of 3 fields in each table. fields I want to change

I'd like the new field names to contain part of the table's file name. For example, for a table named, chirps_1982_02_stat, I could separate out parts of the file name so that the new field names only had the year and month (1982_02) added to them. The desired field names would change from:

  • SUM_grid_code -> Sum_precip_1982_02,
  • MEAN_grid_code -> mean_precip_1982_02
  • STD_grid_code -> std_precip_1982_02.

I'm very new to python so I've found instructions on altering a field name but I'm not sure how to parse out the elements of the file's name and add it to the new field name. I am also not sure how to format the code so that I can do this process 3 times, 1 time for each field name I want to change

What I've found for altering field name

>>> arcpy.env.workspace = r'D:\myfolder\mygdb.gdb'
>>> 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: #loop through each field
...         if field.name.lower() == 'MEAN_grid_code':  #look for the name MEAN_grid_code
...            **arcpy.AlterField_management(fc, field.name, 'Mean Precip %fileyear%', 'Mean Precip XXXX_XX')**

1 Answer 1


First, there is an issue with your AlterField_management(). the third parameter in the method is the new field name which cannot contain spaces or special characters. It would need to be in the format of "Mean_Precip_1982_02".

Second, your if statement "if field.name.lower() == "MEAN_grid_code"". you are never going to have a match on that statement since the string you are comparing it to has capital letters in it.

you would essentially be doing something like the following:

'mean_grid_code' == 'MEAN_grid_code'

which is false.

instead try

field.name.lower() == "mean_grid_code":


field.name == "MEAN_grid_code":

so rewritten:

import arcpy

arcpy.env.workspace = r'D:\myfolder\mygdb.gdb'

for fc in arcpy.ListFeatureClasses():
    for field in arcpy.ListFields(fc):
        file_year = fc.split('_')[-2]
        field_alias = '_'.join(fc.split('_')[-2:])
        if field.name.lower() == "mean_grid_code":
            arcpy.AlterField_management(fc, field.name, 'Mean_Precip_{0}'.format(file_year), 'Mean Precip {0}'.format(field_alias))
        elif field.name.lower() == "sum_grid_code":
            arcpy.AlterField_management(fc, field.name, 'Sum_precip_{0}'.format(file_year), 'Sum precip {0}'.format(field_alias))
        elif field.name.lower() == "std_grid_code":
            arcpy.AlterField_management(fc, field.name, 'std_precip_{0}'.format(file_year), 'std precip {0}'.format(field_alias))
  • Thank you! I've tried the code and I get an error "Parsing error SyntaxError: invalid syntax (line 14) . Not sure what I typed wrong?!
    – Isabel M
    Commented Apr 17, 2018 at 20:26
  • I miss the colon after the else on line 14. It should be else:
    – PMK
    Commented Apr 17, 2018 at 20:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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