Hi.
I´m sure it is easy, but I just can´t make it work...
What I need is to rename fields while exporting feature class to shapefile. There are strings in fields names, that define new names - I want to use dictionary. I know I have to use field mapping. But when I try to overwrite name of output field, it just does not work (names are not overwritten). How to do it?
Sample code:
import arcpy
# input geodatabase
gdb=r"C:\Temp\EXPORT\export.gdb"
# output folder
folder=r"C:\Temp\EXPORT"
# dictionary - string in old name defines new name
fldsNamesDict={'String1':'NewName1','String2':'NewName2','String3':'NewName3','String4':'NewName4'}
# list of strings
fldsNames=list(fldsNamesDict.keys())
arcpy.env.workspace=gdb
fcs=arcpy.ListFeatureClasses()
# loop over feature classes
for fc in fcs:
# new fieldmappings object
fieldmappings=arcpy.FieldMappings()
# load input fc to fieldmappings object
fieldmappings.addTable(fc)
# fields of input fc
flds=fieldmappings.fieldMappings
# loop over fields
for fld in flds:
# loop - which string from dictionary is in old field name? what will be new name?
for fldName in fldsNames:
if fldName in fld.getInputFieldName(0):
# SET NEW FIELD NAME
fld.outputField.name=fldsNamesDict[fldName]
# export fc to shp using field mapping with new fields names
arcpy.FeatureClassToFeatureClass_conversion(fc,folder,fc+".shp","",fieldmappings)Thanks for any help!