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!
Solved! Go to Solution.
Stuck for a long time, solved after few hours after posting into forum ![]()
Two adjustments:
- index and replaceFieldMap
- new "instance" of output field object
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
# INDEX
i=0
# 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
of=fld.outputField
of.name=fldsNamesDict[fldName]
fld.outputField=of
# REPLACE FIELDMAP
fieldmappings.replaceFieldMap (i, fld)
# INCREASING INDEX
i=i+1
# export fc to shp using field mapping with new fields names
arcpy.FeatureClassToFeatureClass_conversion(fc,folder,fc+".shp","",fieldmappings)
You could throw some print statements in there since I can't tell whether anything is being done. For example do you have any confirmation that line 29 ever gets reached, because if the condition in line 27 doesn't work, or isn't met then nothing happens
You are amending a fieldmap object, without then committing the results to the FieldMappings object!
You need to add code like this somewhere:
fieldmappings.replaceFieldMap (index, fld)
Youll have to work out the index bit though!
Stuck for a long time, solved after few hours after posting into forum ![]()
Two adjustments:
- index and replaceFieldMap
- new "instance" of output field object
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
# INDEX
i=0
# 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
of=fld.outputField
of.name=fldsNamesDict[fldName]
fld.outputField=of
# REPLACE FIELDMAP
fieldmappings.replaceFieldMap (i, fld)
# INCREASING INDEX
i=i+1
# export fc to shp using field mapping with new fields names
arcpy.FeatureClassToFeatureClass_conversion(fc,folder,fc+".shp","",fieldmappings)