Hi. I'm having difficulty with field mapping in arcpy - it keeps throwing this error: "The attribute 'outputField' is not supported on this instance of FieldMap." and I'm not sure why. It never manages to get past the first instance, but if I take that part out it complains that streamorder isn't declared. I swear I copied this almost word for word from the documentation, and I'm really not sure why it's failing.
Not sure if this is the right place for this question, so if there's somewhere else I should ask please let me know!
Here's my code:
#setting workspaces and paths
arcpy.env.workspace = path.join(workspace, outgdb, geodataset)
canadafcs = arcpy.ListFeatureClasses()
print("Geodataset and feature classes found.")
#separating feature classes by whether they abbreviate the field name or not
#creating blank lists
fullwords = 
abbrev = 
#running a for loop to get through the feature classes
for fc in canadafcs:
fieldlist = arcpy.ListFields(fc)
if "STREAM_ORDER" in fieldlist:
elif "STRMRDR" in fieldlist:
print("Feature classes separated by field name format.")
#setting up field mappings:
fm_order = arcpy.FieldMap()
fieldmapping = arcpy.FieldMappings()
fm_order.mergeRule = "First"
print("Field mapping parameters set.")
#if the fc uses full words, add the fc and "STREAM_ORDER" as an input field
for fc in fullwords:
#if the fc uses abbreviated words, add the fc and "STRMRDR" as an input field
for fc in abbrev:
print("Input fields added.")
#this is where it throws the error - trying to add streamorder as an output field
streamorder = fm_order.outputField
streamorder.name = "Stream_Order"
fm_order.outputField = streamorder
Not sure of this will help you, but its another example of fieldmapping. I created a function a while ago to assist in feildmappings that are the same name. This takes the target FC, joinfc, and a list of fields that you want to keep from the joinfc.
def fieldMapping(targetfc=None, joinfc=None, keepfields=None): ''' Function to assist in making joins by mapping all the fields in the target fc and including a list of fields that is wanted within the join dataset.. targetfc: featureclass that you want to output joinc: featureclass that you want to join to the target fc keepfields: a list of fields that you want to keep from the joinfc. ''' trglist = [i.name for i in arcpy.ListFields(targetfc)] # List of fields to keep during Join operation fldMap = arcpy.FieldMappings() # Creating field maps for the two files fldMap.addTable(targetfc) fldMap.addTable(joinfc) # add the list of fields from the targetfc to the keepfield list keepfields.extend(trglist) # Removing unwanted fields from the fieldmap for field in fldMap.fields: if field.name not in keepfields: fldMap.removeFieldMap(fldMap.findFieldMapIndex(field.name)) return fldMap
If you have fields that have different names, then after this function you can add them individually by
firstfldmap = fieldMapping(targetfc, joinfc, keepfieldslist) secondfldmap = arcpy.FieldMap() secondfldmap.addInputField(targetfc, "ACCOUNT") secondfldmap.addInputField(joinfc, "accountno") secondfldmap_name = secondfldmap.outputField secondfldmap_name.name = "ACCOUNT" secondfldmap.outputField = secondfldmap_name # then add that field map to the other fieldmap that you returned from the function firstfldmap.addFieldMap(secondfldmap)
Thanks Jeff! I ended up not using field mapping after all - I worked around it by renaming the fields in all my different datasets the same thing so they'd map together automatically. Still not sure what went wrong with my program, but it works now.