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.
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')**