How to match field mappinps with different name

326
3
12-15-2020 12:13 PM
JoseSanchez
Occasional Contributor III

Hello everyone,

 

How can I match  fields that have a different names, for example the same field is called ProjNum in one FC and ProjectNumber in another. 

 

currently I am using this source code to append one FC or Table to another.

fieldmappings = arcpy.FieldMappings()

#Like when you manually choose a layer in the toolbox and it adds the fields to grid


if arcpy.Exists (CurrentFeatureOutput):
fieldmappings.addTable( CurrentFeatureOutput)
fieldmappings.addTable(CurrentFeatureInput )
arcpy.Append_management( CurrentFeatureInput , CurrentFeatureOutput , "NO_TEST", fieldmappings)

 

 

0 Kudos
3 Replies
MehdiPira1
Esri Contributor

Hi @JoseSanchez ,

The link below provides an example that shows how to map fields with different names:

https://community.esri.com/t5/python-questions/append-tool-and-field-mapping-help-examples/td-p/4272...

 

0 Kudos
JoseSanchez
Occasional Contributor III

Thank you   MehdiPira1

I am going to reuse the function posted as sample, it is exactly what I need


@MehdiPira1 wrote:

Hi @JoseSanchez ,

The link below provides an example that shows how to map fields with different names:

https://community.esri.com/t5/python-questions/append-tool-and-field-mapping-help-examples/td-p/4272...

 

def fieldMapping(fc_in,fc_out):
    try:
        # value in the 'in' feature class, value in the feature class to be appended to
        dico = {'Assessed_Value': 'ARN', 'Total_Value': 'TotalValue'}

        fieldmappings = arcpy.FieldMappings()
        fieldmappings.addTable(fc_in)
        fieldmappings.addTable(fc_out)

        for input,output in dico.items():
            field_to_map_index = fieldmappings.findFieldMapIndex(output)
            field_to_map = fieldmappings.getFieldMap(field_to_map_index)
            field_to_map.addInputField(fc_in,input)
            fieldmappings.replaceFieldMap(field_to_map_index, field_to_map)

        return fieldmappings

    except:
        logging.exception('Generic error raised during fieldmap creation')
        raise


 

 

0 Kudos
MehdiPira1
Esri Contributor

Glad to hear that, @JoseSanchez .

Please mark as helpful if you find it helpful. If it answered your question please mark it as answered.

Thanks

0 Kudos