How to use Field Mappings

5183
2
09-14-2015 11:48 AM
mpboyle
Occasional Contributor III

I'm trying to understand how to use the FieldMappings object in arcpy, but am getting confused.

I have two tables, an input table and a target table.  I'm trying to use the Append tool to load data from the input table into the target table.  I have two fields within each table that need to be mapped.

Input

NEAR_FID

BUFF_DIST

Target

WellOID

Distance

Mapping should be:

NEAR_FID = WellOID

BUFF_DIST = Distance

Any suggestions on how to map these fields using the FieldMappings object would be greatly appreciated!

Tags (1)
0 Kudos
2 Replies
WesMiller
Regular Contributor III

Here is how I'm using field mappings for my street index creator hope this helps

http://desktop.arcgis.com/en/desktop/latest/analyze/arcpy-classes/fieldmappings.htm

# Create a new fieldmappings and add the two input feature classes.
fieldmappings = arcpy.FieldMappings()
fieldmappings.addTable(DissolvedStreets)
fieldmappings.addTable(InputGrid)


jField = fieldmappings.findFieldMapIndex(gridField)
fieldmap = fieldmappings.getFieldMap(jField)


fieldmap.mergeRule = "join"
fieldmap.joinDelimiter = ","
fld_length = fieldmap.outputField
fld_length.length = 255
fieldmap.outputField = fld_length
arcpy.AddMessage(str(fieldmap.outputField.length))
fieldmappings.replaceFieldMap(jField, fieldmap)


# Process: Spatial Join
arcpy.SpatialJoin_analysis(DissolvedStreets, InputGrid, outfc, "#", "#", fieldmappings)
0 Kudos
MahtabAlam1
Occasional Contributor

Following might help a bit:

# define new mappings and load default from fc or table
fieldmappings = arcpy.FieldMappings()
fieldmappings.addTable(yourFCorTable)


# now you define the ouput fields
fieldmap1 = fieldmappings.getFieldMap(1)
outField1 = fieldmap.outputField
outField1.name = "WellOID"
outField1.aliasName = "WellOID"
fieldmap1.outputField = outField1


fieldmap2 = fieldmappings.getFieldMap(2)
outField2 = fieldmap2.outputField
outField2.name = "Distance"
outField2.aliasName = "Distance"
fieldmap2.outputField = outField2


# now you can use your field mappings
0 Kudos