I have several fieldmapping snippets that I refer to often for this purpose. It really helps when doing spatial joins and only keeping the fields that you want to bring over or exporting only the fields you want without needing to do a delete fields after export. Was going to post this for clt_cabq but ran out of time. 4 variations of the fieldmapping use:
"""
Keeping only a selected list of fields:
Creates a fieldmap that keeps all of the output fields listed in the keepfields:
"""
fc = r'C:\Users\...\Documents\ArcGIS\Local.gdb\address'
fieldmappings = arcpy.FieldMappings()
fieldmappings.addTable(fc)
keepfields = ['address','bldg_no','space','units']
# Removing unwanted fields
for field in fieldmappings.fields:
if all([not field.required, field.name not in keepfields]):
fieldmappings.removeFieldMap(fieldmappings.findFieldMapIndex(field.name))
arcpy.FeatureClassToFeatureClass_conversion(fc, r'C:\Users\...\Documents\ArcGIS\Projects\Testing', 'adr.shp', '', fieldmappings)
"""
Keeping only the changed fields version:
Creates a field map between a two featureclasses and keeps only fields that are required and
maps the incoming names to new field names in the destination fc.
"""
fc1 = r'C:\Users\...\Documents\ArcGIS\Local.gdb\address'
fc2 = r'C:\Users\...\Documents\ArcGIS\Local.gdb\parcel'
# {incoming field name: desired name}
fldPair = {'AddressNum': 'bldg_no',
'PreDirecti': 'st_dir',
'PreType': 'st_type',
'StreetName': 'st_name',
'PostDirect': 'suf_dir',
'Unit': 'units',
'City': 'town',
'ZipCode': 'zip',
'FullAddres': 'address'}
fldmap = arcpy.FieldMappings()
# Creating field maps for the two files
fldmap.addTable(fc1)
fldmap.addTable(fc2)
# Removing all unwanted fields
for field in fldmap.fields:
if not field.required:
fldmap.removeFieldMap(fldmap.findFieldMapIndex(field.name))
# create field map using fields from the incoming fc (ifld) to fields in the target
# tfld
for ifld, tfld in fldPair.items():
ufldmap = arcpy.FieldMap()
ufldmap.addInputField(fc1, ifld)
ufldmap.addInputField(fc2, tfld)
# ufldmap.mergeRule = "Unique"
ufldmap_name = ufldmap.outputField
ufldmap_name.name = tfld
ufldmap.outputField = ufldmap_name
fldmap.addFieldMap(ufldmap)
arcpy.Append_management(fc2, fc1, field_mapping= fldmap)
"""
Keeping all mapped fields, but changing some field names version:
Creates a field map between the targetfc and incomingfc and changes the mapping
of the fields listed in the fldPair dictionary
"""
fc1 = r'C:\Users\...\Documents\ArcGIS\Local.gdb\address'
fc2 = r'C:\Users\...\Documents\ArcGIS\Local.gdb\parcel'
# {incoming field name: targetFC field}
fldPair = {'AddressNum': 'bldg_no',
'PreDirecti': 'st_dir',
'PreType': 'st_type',
'StreetName': 'st_name',
'PostDirect': 'suf_dir',
'Unit': 'units',
'City': 'town',
'ZipCode': 'zip',
'FullAddres': 'address'}
fldmap = arcpy.FieldMappings()
# Creating field maps for the two files
fldmap.addTable(fc1)
fldmap.addTable(fc2)
# Removing fields whose names will be changed.
for field in fldmap.fields:
if field.name in fldPair.keys():
fldmap.removeFieldMap(fldmap.findFieldMapIndex(field.name))
# create field map using fields from the incoming fc (ifld) to fields in the target
# tfld
for ifld, tfld in fldPair.items():
ufldmap = arcpy.FieldMap()
ufldmap.addInputField(fc1, ifld)
ufldmap.addInputField(fc2, tfld)
# ufldmap.mergeRule = "Unique"
ufldmap_name = ufldmap.outputField
ufldmap_name.name = tfld
ufldmap.outputField = ufldmap_name
fldmap.addFieldMap(ufldmap)
arcpy.SpatialJoin_analysis(fc2, fc1, out_fc, 'INTERSECT', field_mapping= fldmap)
"""
Renaming fields version of single featureclass (for exporting):
Creates a fieldmap of output fields and maps the input fields names.
"""
fc = r'C:\Users\..\Documents\ArcGIS\Projects\Testing\Default.gdb\team_city'
out_features = r'C:\Users\..\Documents\ArcGIS\Projects\Testing\Default.gdb\team_city_update'
fieldmappings = arcpy.FieldMappings()
fieldmappings.addTable(fc)
# {incoming field name : targetFC field}
fldPair = {'UIDENT': 'bldg_no',
'POPCLASS': 'st_dir',
'STATEABB': 'st_type'}
for kfld, tfld in fldPair.items():
fldmp = fieldmappings.getFieldMap(fieldmappings.findFieldMapIndex(kfld))
ufldmap_name = fldmp.outputField
ufldmap_name.name = tfld
ufldmap_name.aliasName = tfld
fldmp.outputField = ufldmap_name
fieldmappings.replaceFieldMap(fieldmappings.findFieldMapIndex(kfld), fldmp)
arcpy.conversion.ExportFeatures(fc, out_features, field_mapping=fieldmappings)
Hope this helps- If you can get into the fieldmap object in the debugger, you can see how things work and what other properties you can set using these methods.