Appending: different input field based on value?

Discussion created by johnson_t_j on Feb 28, 2011
I was wondering if it???s possible to determine the input field while appending based on an input value. I have 2 address fields and a preference field in an input shapefile. Based on the value of the preference field, one address field will be the ???Final Address??? and the other will be the ???Alias Address??? in the output. I'm new to Python but am trying to automate this process (and include it in a larger model) to save some time.

Some background:
I have multiple shapefiles containing address points for each county in the state. The field names for these address points were derived from a parcel file and unfortunately the names of the address fields differ from county to county.
I want to append each input shapefile to a feature class template that has the field names and field order that I require.

I have looked into the FieldMapping and can figure out how to get most of the fields correctly mapped. I have 2 address fields in my input shapfile: an "Original" address and a "Standardized" address. I was planning on using the GetParameterAsText to set the input file's "Original" address when I start the tool  as this field name often differs between counties (the "Standardized" field is always named the same). In the template, the 2 address fields will be "Final_Addr" and "Alias_Addr".

In my input shapefile, I have a Preference field that determines which of the address fields will be used as the final address in the template:
If "Preference" = "O", then input's "Original" = template???s "Final_Addr" and the input's "Standardized" = template???s "Alias_Addr"
If "Preference" = "S", then input's "Standardized" = "Final_Addr" and the input's "Original" = "Alias_Addr"
If "Preference" = "N/A" (there is no difference between the original and standardized), then input's "Original" = "Final_Addr" and "Alias_Addr" will be left blank.

Below is what I have so far, I'm just not sure how to include the if/then Preference statement to my field mapping list.

import arcpy
arcpy.env.workspace = "C:/Geocoding/Python_Scripts/Cherokee_test"

#Set input address shapefile and feature class template
input_shp = "Cherokee_ Address.shp"
template = arcpy.env.workspace + "/Cherokee_Template.gdb/Address_template"

#Set ???Original??? address field from input shapefile
orig_addr = GetParameterAsText(0)

# From here I???m not sure how to proceed with the FieldMapping

fieldmappings = arcpy.CreateObject("FieldMappings")
#Examples of my mapped fields that won't change
fieldmap_list = [("CITY", "COMMUNITY"), \
                 ("OID_", "LINK_ID")]
for (inputfield, outputfield) in fieldmap_list:
    fieldmap = fieldmappings.GetFieldMap(fieldmappings.FindFieldMapIndex(outputfield))
    fieldmap.addInputField(input_shp, inputfield)
    fieldmappings.ReplaceFieldMap(fieldmappings.FindFieldMapIndex(outputfield), fieldmap)

arcpy. Append_management(template, input_shp, "NO_TEST", fieldmappings)

Any help or advice would be greatly appreciated. Thanks in advance!