{"id":181701,"date":"2012-08-30T23:14:38","date_gmt":"2012-08-31T06:14:38","guid":{"rendered":"http:\/\/www.esri.com\/arcgis-blog\/?post_type=blog&#038;p=181701"},"modified":"2018-12-18T11:21:39","modified_gmt":"2018-12-18T19:21:39","slug":"field-mapping-and-python-scripting","status":"publish","type":"blog","link":"https:\/\/www.esri.com\/arcgis-blog\/products\/arcgis-desktop\/analytics\/field-mapping-and-python-scripting","title":{"rendered":"Field mapping and Python scripting"},"author":5181,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","format":"standard","meta":{"_acf_changed":false,"_searchwp_excluded":""},"categories":[23341],"tags":[24321,24341,24351],"industry":[],"product":[36991],"class_list":["post-181701","blog","type-blog","status-publish","format-standard","hentry","category-analytics","tag-geoprocessing","tag-python","tag-scripting","product-arcgis-desktop"],"acf":{"short_description":"A number of geoprocessing tools including Spatial Join (Analysis), Append (Management), Merge (Management), Feature Class To Feature Class","flexible_content":[{"acf_fc_layout":"content","content":"<p>A number of geoprocessing tools including\u00a0<a title=\"Spatial Join (Analysis)\" href=\"http:\/\/resources.arcgis.com\/en\/help\/main\/10.1\/index.html#\/\/00080000000q000000\">Spatial Join (Analysis)<\/a>,\u00a0<a href=\"http:\/\/resources.arcgis.com\/en\/help\/main\/10.1\/index.html#\/\/001700000050000000\">Append (Management)<\/a>,\u00a0<a href=\"http:\/\/resources.arcgis.com\/en\/help\/main\/10.1\/index.html#\/Merge\/001700000055000000\/\">Merge (Management)<\/a>,\u00a0<a href=\"http:\/\/resources.arcgis.com\/en\/help\/main\/10.1\/index.html#\/\/001200000020000000\">Feature Class To Feature Class (Conversion)<\/a>, and\u00a0<a href=\"http:\/\/resources.arcgis.com\/en\/help\/main\/10.1\/index.html#\/Table_to_Table\/001200000027000000\/\">Table To Table (Conversion)<\/a>, have a parameter for controlling how fields from the input dataset(s) are processed and written, or mapped, to the output dataset \u2013 the\u00a0<a href=\"http:\/\/resources.arcgis.com\/en\/help\/main\/10.1\/index.html#\/\/00210000000s000000\">Field Map<\/a>\u00a0parameter. In addition to the simple moving of attributes from input to output, field mapping can also be useful for some common tasks such as field concatenation and calculating statistics like mean, sum, and standard deviation.<\/p>\n<div id=\"attachment_19152\" class=\"wp-caption aligncenter\">\n<p><a href=\"http:\/\/blogs.esri.com\/esri\/arcgis\/files\/2012\/08\/image1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-19152\" src=\"http:\/\/blogs.esri.com\/esri\/arcgis\/files\/2012\/08\/image1.png\" alt=\"\" width=\"369\" height=\"158\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Field Map in the Append (Managemnet) tool dialog<\/p>\n<\/div>\n<p>If you haven\u2019t used the Field Map before, you should! Understanding and using field mapping will often reduce the number of processing steps in a workflow, and ensure that, in any scenario, attributes are handled in an appropriate way. Yes, the Field Map parameter is a complicated one, but it is well worth the time it takes to figure it out.<\/p>\n<p>Because the Field Map is a complicated parameter, working with it in Python can also be complicated. The best way to interact with field mapping in Python scripting is with the\u00a0<a href=\"http:\/\/resources.arcgis.com\/en\/help\/main\/10.1\/index.html#\/\/018z00000078000000\">FieldMappings<\/a>\u00a0object. In Python, most geoprocessing tool parameter types are seen as simple numbers or strings (specifying a feature class input is as easy as providing the feature class\u2019 path). But several of the more complex parameters have objects that exist to help you effectively work with the parameter. The Field Map parameter\u00a0<em>can<\/em>\u00a0accept a long structured string indicating the field map settings (you may have seen this long string equivalent in geoprocessing messages), however, working with the field mapping string is inefficient and error-prone, so use the FieldMappings object for the best experience.<\/p>\n<div id=\"attachment_19160\" class=\"wp-caption aligncenter\">\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-19160\" src=\"http:\/\/blogs.esri.com\/esri\/arcgis\/files\/2012\/08\/image2.png\" alt=\"\" width=\"671\" height=\"117\" \/><\/p>\n<p class=\"wp-caption-text\">String equivalent of Field Map parameter<\/p>\n<\/div>\n<div id=\"attachment_19161\" class=\"wp-caption aligncenter\">\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-19161 \" src=\"http:\/\/blogs.esri.com\/esri\/arcgis\/files\/2012\/08\/image3.png\" alt=\"\" width=\"408\" height=\"157\" \/><\/p>\n<p class=\"wp-caption-text\">FieldMappings object has many properties and methods to efficiently work with field mapping<\/p>\n<\/div>\n<h1><strong>Problem<\/strong><\/h1>\n<p>I was recently presented with a data migration problem where field mappings and Python scripting literally saved me weeks of work. The goal was to convert a collection of hundreds of VPF (Vector Product Format) databases containing many feature classes to a handful of geodatabases, and because of the large scale of the migration it had to be accomplished in an automated fashion (this is where the many weeks of work would be saved). The schema of the geodatabases was already set up with a number of feature datasets and empty feature classes into which the VPF feature class data would be imported using the Append (management) tool.<\/p>\n<p>The iteration through the collection of VPF databases was solved with some simple looping techniques involving the\u00a0<code><a href=\"http:\/\/resources.arcgis.com\/en\/help\/main\/10.1\/index.html#\/\/018v00000014000000\">arcpy.ListDatasets()<\/a><\/code>and\u00a0<code><a href=\"http:\/\/resources.arcgis.com\/en\/help\/main\/10.1\/index.html#\/\/018v00000018000000\">arcpy.ListFeatureClasses()<\/a><\/code>\u00a0functions. \u00a0However, there was a fundamental problem that nearly derailed the automation of this process: VPF feature classes can have spaces in their field names, while geodatabase datasets cannot. When the empty geodatabase feature classes were created from the schema of the VPF feature classes, the spaces in the field names were automatically changed to underscores ( _ ) in the geodatabase feature classes. This very subtle difference caused huge ripples in the automated process, since the Append (Management) tool can not automatically match fields like \u2018mcc description\u2019 to \u2018mcc_description\u2019; in the output geodatabase feature class, all the values in the \u2018mcc_description\u2019 field are NULL because the fields were not matched.<\/p>\n<div id=\"attachment_19162\" class=\"wp-caption aligncenter\">\n<p><a href=\"http:\/\/blogs.esri.com\/esri\/arcgis\/files\/2012\/08\/image4.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-19162 \" src=\"http:\/\/blogs.esri.com\/esri\/arcgis\/files\/2012\/08\/image4.png\" alt=\"\" width=\"417\" height=\"205\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">VPF and geodatabase feature class fields<\/p>\n<\/div>\n<p>Viewing the Field Map parameter on the Append (Management) tool helps illustrate the issue. The fields \u2018f_code description\u2019, \u2018mcc description\u2019, and \u2018mcs description\u2019 are not automatically matched to the geodatabase target schema because of the difference of the space and underscore characters.<\/p>\n<div id=\"attachment_19164\" class=\"wp-caption aligncenter\">\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-19164\" src=\"http:\/\/blogs.esri.com\/esri\/arcgis\/files\/2012\/08\/image5.png\" alt=\"\" width=\"197\" height=\"202\" \/><\/p>\n<p class=\"wp-caption-text\">Append tool default Field Map, showing the target geodatabase feature class schema<\/p>\n<\/div>\n<h1><strong>Solution<\/strong><\/h1>\n<p>This situation can be rectified by manually adding the matching input fields using the Field Map controls on the Append tool. For each field that does not have a match, right-click and add the appropriate input field from the VPF feature class.<\/p>\n<p>&nbsp;<\/p>\n<div id=\"attachment_19159\" class=\"wp-caption aligncenter\">\n<p><a href=\"http:\/\/blogs.esri.com\/esri\/arcgis\/files\/2012\/08\/image6.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-19159 \" src=\"http:\/\/blogs.esri.com\/esri\/arcgis\/files\/2012\/08\/image6.png\" alt=\"\" width=\"577\" height=\"191\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Use the Field Map controls to do manual field mapping<\/p>\n<\/div>\n<p>These manual steps resulted in a correct field mapping and import of the VPF feature class data to geodatabase\u00a0<em>for a single VPF feature class<\/em>. To successfully perform the automated migration, I incorporated this additional field mapping work into the migration script using the FieldMappings object. For each iteration before a VPF feature class is appended into a geodatabase feature class, the script iterates through each VPF feature class field, does a find and replace for those fields with a space and the corresponding field with an underscore, and matches the two fields in the FieldMappings object. A good way to understand the usage of the FieldMappings object is to think about these steps in the same context as when the field mapping was done in the tool dialog: the script goes through each output field in the Field Map (these are the fields from the Target Dataset), and adds an input field from the VPF feature class that matches, regardless of the difference with space and underscore characters, before finally executing the Append process with the customized field mapping.<\/p>\n<div>\n<div id=\"highlighter_36430\" class=\"syntaxhighlighter python\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td class=\"gutter\">\n<div class=\"line number1 index0 alt2\">1<\/div>\n<div class=\"line number2 index1 alt1\">2<\/div>\n<div class=\"line number3 index2 alt2\">3<\/div>\n<div class=\"line number4 index3 alt1\">4<\/div>\n<div class=\"line number5 index4 alt2\">5<\/div>\n<div class=\"line number6 index5 alt1\">6<\/div>\n<div class=\"line number7 index6 alt2\">7<\/div>\n<div class=\"line number8 index7 alt1\">8<\/div>\n<div class=\"line number9 index8 alt2\">9<\/div>\n<div class=\"line number10 index9 alt1\">10<\/div>\n<div class=\"line number11 index10 alt2\">11<\/div>\n<div class=\"line number12 index11 alt1\">12<\/div>\n<div class=\"line number13 index12 alt2\">13<\/div>\n<div class=\"line number14 index13 alt1\">14<\/div>\n<div class=\"line number15 index14 alt2\">15<\/div>\n<div class=\"line number16 index15 alt1\">16<\/div>\n<div class=\"line number17 index16 alt2\">17<\/div>\n<div class=\"line number18 index17 alt1\">18<\/div>\n<div class=\"line number19 index18 alt2\">19<\/div>\n<div class=\"line number20 index19 alt1\">20<\/div>\n<div class=\"line number21 index20 alt2\">21<\/div>\n<div class=\"line number22 index21 alt1\">22<\/div>\n<div class=\"line number23 index22 alt2\">23<\/div>\n<div class=\"line number24 index23 alt1\">24<\/div>\n<div class=\"line number25 index24 alt2\">25<\/div>\n<div class=\"line number26 index25 alt1\">26<\/div>\n<div class=\"line number27 index26 alt2\">27<\/div>\n<div class=\"line number28 index27 alt1\">28<\/div>\n<div class=\"line number29 index28 alt2\">29<\/div>\n<div class=\"line number30 index29 alt1\">30<\/div>\n<div class=\"line number31 index30 alt2\">31<\/div>\n<div class=\"line number32 index31 alt1\">32<\/div>\n<div class=\"line number33 index32 alt2\">33<\/div>\n<div class=\"line number34 index33 alt1\">34<\/div>\n<div class=\"line number35 index34 alt2\">35<\/div>\n<div class=\"line number36 index35 alt1\">36<\/div>\n<div class=\"line number37 index36 alt2\">37<\/div>\n<div class=\"line number38 index37 alt1\">38<\/div>\n<div class=\"line number39 index38 alt2\">39<\/div>\n<div class=\"line number40 index39 alt1\">40<\/div>\n<div class=\"line number41 index40 alt2\">41<\/div>\n<div class=\"line number42 index41 alt1\">42<\/div>\n<div class=\"line number43 index42 alt2\">43<\/div>\n<div class=\"line number44 index43 alt1\">44<\/div>\n<div class=\"line number45 index44 alt2\">45<\/div>\n<div class=\"line number46 index45 alt1\">46<\/div>\n<div class=\"line number47 index46 alt2\">47<\/div>\n<div class=\"line number48 index47 alt1\">48<\/div>\n<div class=\"line number49 index48 alt2\">49<\/div>\n<div class=\"line number50 index49 alt1\">50<\/div>\n<div class=\"line number51 index50 alt2\">51<\/div>\n<div class=\"line number52 index51 alt1\">52<\/div>\n<div class=\"line number53 index52 alt2\">53<\/div>\n<div class=\"line number54 index53 alt1\">54<\/div>\n<\/td>\n<td class=\"code\">\n<div class=\"container\">\n<div class=\"line number1 index0 alt2\"><code class=\"python keyword\">import<\/code> <code class=\"python plain\">arcpy<\/code><\/div>\n<div class=\"line number2 index1 alt1\"><code class=\"python keyword\">import<\/code> <code class=\"python plain\">os<\/code><\/div>\n<div class=\"line number3 index2 alt2\"><\/div>\n<div class=\"line number4 index3 alt1\"><code class=\"python plain\">folder <\/code><code class=\"python keyword\">=<\/code> <code class=\"python plain\">r<\/code><code class=\"python string\">\"C:testingfieldmapautodnc13\"<\/code><\/div>\n<div class=\"line number5 index4 alt2\"><code class=\"python plain\">gdbfolder <\/code><code class=\"python keyword\">=<\/code> <code class=\"python plain\">r<\/code><code class=\"python string\">\"C:testingfieldmapautogdbs\"<\/code><\/div>\n<div class=\"line number6 index5 alt1\"><\/div>\n<div class=\"line number7 index6 alt2\"><code class=\"python plain\">arcpy.env.workspace <\/code><code class=\"python keyword\">=<\/code> <code class=\"python plain\">folder<\/code><\/div>\n<div class=\"line number8 index7 alt1\"><code class=\"python plain\">listvpf <\/code><code class=\"python keyword\">=<\/code> <code class=\"python plain\">arcpy.ListDatasets()<\/code><\/div>\n<div class=\"line number9 index8 alt2\"><\/div>\n<div class=\"line number10 index9 alt1\"><code class=\"python keyword\">for<\/code> <code class=\"python plain\">vpf <\/code><code class=\"python keyword\">in<\/code> <code class=\"python plain\">listvpf:<\/code><\/div>\n<div class=\"line number11 index10 alt2\"><code class=\"python spaces\">\u00a0\u00a0\u00a0\u00a0<\/code><code class=\"python comments\"># First character of the VPF determines the geodatabase to append to<\/code><\/div>\n<div class=\"line number12 index11 alt1\"><code class=\"python spaces\">\u00a0\u00a0\u00a0\u00a0<\/code><code class=\"python comments\">#<\/code><\/div>\n<div class=\"line number13 index12 alt2\"><code class=\"python spaces\">\u00a0\u00a0\u00a0\u00a0<\/code><code class=\"python keyword\">if<\/code> <code class=\"python plain\">vpf[<\/code><code class=\"python value\">0<\/code><code class=\"python plain\">] <\/code><code class=\"python keyword\">=<\/code><code class=\"python keyword\">=<\/code> <code class=\"python string\">\"h\"<\/code><code class=\"python plain\">:<\/code><\/div>\n<div class=\"line number14 index13 alt1\"><code class=\"python spaces\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code class=\"python plain\">gdb <\/code><code class=\"python keyword\">=<\/code> <code class=\"python plain\">os.path.join(gdbfolder, <\/code><code class=\"python string\">\"DNCHarbor.gdb\"<\/code><code class=\"python plain\">)<\/code><\/div>\n<div class=\"line number15 index14 alt2\"><code class=\"python spaces\">\u00a0\u00a0\u00a0\u00a0<\/code><code class=\"python keyword\">elif<\/code> <code class=\"python plain\">vpf[<\/code><code class=\"python value\">0<\/code><code class=\"python plain\">] <\/code><code class=\"python keyword\">=<\/code><code class=\"python keyword\">=<\/code> <code class=\"python string\">\"a\"<\/code><code class=\"python plain\">:<\/code><\/div>\n<div class=\"line number16 index15 alt1\"><code class=\"python spaces\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code class=\"python plain\">gdb <\/code><code class=\"python keyword\">=<\/code> <code class=\"python plain\">os.path.join(gdbfolder, <\/code><code class=\"python string\">\"DNCApproach.gdb\"<\/code><code class=\"python plain\">)<\/code><\/div>\n<div class=\"line number17 index16 alt2\"><code class=\"python spaces\">\u00a0\u00a0\u00a0\u00a0<\/code><code class=\"python keyword\">elif<\/code> <code class=\"python plain\">vpf[<\/code><code class=\"python value\">0<\/code><code class=\"python plain\">] <\/code><code class=\"python keyword\">=<\/code><code class=\"python keyword\">=<\/code> <code class=\"python string\">\"c\"<\/code><code class=\"python plain\">:<\/code><\/div>\n<div class=\"line number18 index17 alt1\"><code class=\"python spaces\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code class=\"python plain\">gdb <\/code><code class=\"python keyword\">=<\/code> <code class=\"python plain\">os.path.join(gdbfolder, <\/code><code class=\"python string\">\"DNCCoastal.gdb\"<\/code><code class=\"python plain\">)<\/code><\/div>\n<div class=\"line number19 index18 alt2\"><code class=\"python spaces\">\u00a0\u00a0\u00a0\u00a0<\/code><code class=\"python keyword\">elif<\/code> <code class=\"python plain\">vpf[<\/code><code class=\"python value\">0<\/code><code class=\"python plain\">] <\/code><code class=\"python keyword\">=<\/code><code class=\"python keyword\">=<\/code> <code class=\"python string\">\"g\"<\/code><code class=\"python plain\">:<\/code><\/div>\n<div class=\"line number20 index19 alt1\"><code class=\"python spaces\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code class=\"python plain\">gdb <\/code><code class=\"python keyword\">=<\/code> <code class=\"python plain\">os.path.join(gdbfolder, <\/code><code class=\"python string\">\"DNCGeneral.gdb\"<\/code><code class=\"python plain\">)<\/code><\/div>\n<div class=\"line number21 index20 alt2\"><\/div>\n<div class=\"line number22 index21 alt1\"><code class=\"python spaces\">\u00a0\u00a0\u00a0\u00a0<\/code><code class=\"python comments\"># The characters after the : in the VPF name determine the feature dataset to append to<\/code><\/div>\n<div class=\"line number23 index22 alt2\"><code class=\"python spaces\">\u00a0\u00a0\u00a0\u00a0<\/code><code class=\"python comments\">#<\/code><\/div>\n<div class=\"line number24 index23 alt1\"><code class=\"python spaces\">\u00a0\u00a0\u00a0\u00a0<\/code><code class=\"python plain\">fd <\/code><code class=\"python keyword\">=<\/code> <code class=\"python plain\">vpf.split(<\/code><code class=\"python string\">\":\"<\/code><code class=\"python plain\">)[<\/code><code class=\"python value\">1<\/code><code class=\"python plain\">]<\/code><\/div>\n<div class=\"line number25 index24 alt2\"><code class=\"python spaces\">\u00a0\u00a0\u00a0\u00a0<\/code><code class=\"python plain\">arcpy.env.workspace <\/code><code class=\"python keyword\">=<\/code> <code class=\"python plain\">os.path.join(folder,vpf)<\/code><\/div>\n<div class=\"line number26 index25 alt1\"><code class=\"python spaces\">\u00a0\u00a0\u00a0\u00a0<\/code><code class=\"python plain\">listvpffc <\/code><code class=\"python keyword\">=<\/code> <code class=\"python plain\">arcpy.ListFeatureClasses()<\/code><\/div>\n<div class=\"line number27 index26 alt2\"><code class=\"python spaces\">\u00a0\u00a0\u00a0\u00a0<\/code><code class=\"python keyword\">for<\/code> <code class=\"python plain\">fc <\/code><code class=\"python keyword\">in<\/code> <code class=\"python plain\">listvpffc:<\/code><\/div>\n<div class=\"line number28 index27 alt1\"><code class=\"python spaces\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code class=\"python plain\">targetd <\/code><code class=\"python keyword\">=<\/code> <code class=\"python plain\">os.path.join(gdbfolder,gdb,fd,fc)<\/code><\/div>\n<div class=\"line number29 index28 alt2\"><\/div>\n<div class=\"line number30 index29 alt1\"><code class=\"python spaces\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code class=\"python comments\"># Create FieldMappings object and load the target dataset<\/code><\/div>\n<div class=\"line number31 index30 alt2\"><code class=\"python spaces\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code class=\"python comments\">#<\/code><\/div>\n<div class=\"line number32 index31 alt1\"><code class=\"python spaces\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code class=\"python plain\">fieldmappings <\/code><code class=\"python keyword\">=<\/code> <code class=\"python plain\">arcpy.FieldMappings()<\/code><\/div>\n<div class=\"line number33 index32 alt2\"><code class=\"python spaces\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code class=\"python plain\">fieldmappings.addTable(targetd)<\/code><\/div>\n<div class=\"line number34 index33 alt1\"><\/div>\n<div class=\"line number35 index34 alt2\"><code class=\"python spaces\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code class=\"python comments\"># Loop through each field in the input dataset<\/code><\/div>\n<div class=\"line number36 index35 alt1\"><code class=\"python spaces\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code class=\"python comments\">#<\/code><\/div>\n<div class=\"line number37 index36 alt2\"><code class=\"python spaces\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code class=\"python plain\">inputfields <\/code><code class=\"python keyword\">=<\/code> <code class=\"python plain\">[field.name <\/code><code class=\"python keyword\">for<\/code> <code class=\"python plain\">field <\/code><code class=\"python keyword\">in<\/code> <code class=\"python plain\">arcpy.ListFields(fc) <\/code><code class=\"python keyword\">if<\/code> <code class=\"python keyword\">not<\/code> <code class=\"python plain\">field.required]<\/code><\/div>\n<div class=\"line number38 index37 alt1\"><code class=\"python spaces\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code class=\"python keyword\">for<\/code> <code class=\"python plain\">inputfield <\/code><code class=\"python keyword\">in<\/code> <code class=\"python plain\">inputfields:<\/code><\/div>\n<div class=\"line number39 index38 alt2\"><code class=\"python spaces\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code class=\"python comments\"># Iterate through each FieldMap in the FieldMappings<\/code><\/div>\n<div class=\"line number40 index39 alt1\"><code class=\"python spaces\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code class=\"python comments\">#<\/code><\/div>\n<div class=\"line number41 index40 alt2\"><code class=\"python spaces\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code class=\"python keyword\">for<\/code> <code class=\"python plain\">i <\/code><code class=\"python keyword\">in<\/code> <code class=\"python functions\">range<\/code><code class=\"python plain\">(fieldmappings.fieldCount):<\/code><\/div>\n<div class=\"line number42 index41 alt1\"><code class=\"python spaces\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code class=\"python plain\">fieldmap <\/code><code class=\"python keyword\">=<\/code> <code class=\"python plain\">fieldmappings.getFieldMap(i)<\/code><\/div>\n<div class=\"line number43 index42 alt2\"><code class=\"python spaces\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code class=\"python comments\"># If the field name from the target dataset matches to a validated input field name<\/code><\/div>\n<div class=\"line number44 index43 alt1\"><code class=\"python spaces\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code class=\"python comments\"># <\/code><\/div>\n<div class=\"line number45 index44 alt2\"><code class=\"python spaces\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code class=\"python keyword\">if<\/code> <code class=\"python plain\">fieldmap.getInputFieldName(<\/code><code class=\"python value\">0<\/code><code class=\"python plain\">) <\/code><code class=\"python keyword\">=<\/code><code class=\"python keyword\">=<\/code> <code class=\"python plain\">inputfield.replace(<\/code><code class=\"python string\">\" \"<\/code><code class=\"python plain\">, <\/code><code class=\"python string\">\"_\"<\/code><code class=\"python plain\">):<\/code><\/div>\n<div class=\"line number46 index45 alt1\"><code class=\"python spaces\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code class=\"python comments\"># Add the input field to the FieldMap and replace the old FieldMap with the new<\/code><\/div>\n<div class=\"line number47 index46 alt2\"><code class=\"python spaces\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code class=\"python comments\"># <\/code><\/div>\n<div class=\"line number48 index47 alt1\"><code class=\"python spaces\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code class=\"python plain\">fieldmap.addInputField(fc, inputfield)<\/code><\/div>\n<div class=\"line number49 index48 alt2\"><code class=\"python spaces\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code class=\"python plain\">fieldmappings.replaceFieldMap(i, fieldmap)<\/code><\/div>\n<div class=\"line number50 index49 alt1\"><code class=\"python spaces\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code class=\"python keyword\">break<\/code><\/div>\n<div class=\"line number51 index50 alt2\"><\/div>\n<div class=\"line number52 index51 alt1\"><code class=\"python spaces\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code class=\"python comments\"># Perform the Append<\/code><\/div>\n<div class=\"line number53 index52 alt2\"><code class=\"python spaces\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code class=\"python comments\"># <\/code><\/div>\n<div class=\"line number54 index53 alt1\"><code class=\"python spaces\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code class=\"python plain\">arcpy.management.Append(fc, targetd, <\/code><code class=\"python string\">\"NO_TEST\"<\/code><code class=\"python plain\">, fieldmappings)<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<h1><strong>Summary<\/strong><\/h1>\n<p>Several geoprocessing tools use the complex but powerful Field Map parameter to control how attributes from the input dataset(s) are mapped to the output dataset. Understanding and using the field mapping parameter on geoprocessing tools can enable time-saving workflows, will often reduce the number of required processing steps, and ensures that in any scenario attributes are never lost and always handled in an appropriate way. In Python scripting, the FieldMappings object provides efficient,\u00a0<em>automatable<\/em>\u00a0access to all the same functionality as the Field Map control on the tool dialog, and can be used in many scenarios including programmatically matching fields with different names.<\/p>\n<p>Use field mapping, it will make your job easier!<\/p>\n"}],"authors":[{"ID":5181,"user_firstname":"Drew","user_lastname":"Flater","nickname":"Drew Flater","user_nicename":"drewf","display_name":"Drew Flater","user_email":"dflater@esri.com","user_url":"","user_registered":"2018-03-02 00:16:51","user_description":"Group Lead Product Engineer for the Geoprocessing development team at Esri, leading and contributing to projects across ArcGIS products with focus on ArcGIS Pro.\r\nDrew joined Esri in 2008, after completing his Masters in GIS at the University of Akron. Now he designs, codes, tests, and documents tools that can be used to understand and find meaning in raw geospatial data. He works to make Esri's geoprocessing and analytical capabilities easy to use and automate.","user_avatar":"<img data-del=\"avatar\" src='https:\/\/www.esri.com\/arcgis-blog\/app\/uploads\/2024\/05\/drew-465x465.jpg' class='avatar pp-user-avatar avatar-96 photo ' height='96' width='96'\/>"}],"related_articles":"","card_image":false,"wide_image":false},"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v25.9 (Yoast SEO v25.9) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Field mapping and Python scripting<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.esri.com\/arcgis-blog\/products\/arcgis-desktop\/analytics\/field-mapping-and-python-scripting\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Field mapping and Python scripting\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.esri.com\/arcgis-blog\/products\/arcgis-desktop\/analytics\/field-mapping-and-python-scripting\" \/>\n<meta property=\"og:site_name\" content=\"ArcGIS Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/esrigis\/\" \/>\n<meta property=\"article:modified_time\" content=\"2018-12-18T19:21:39+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:site\" content=\"@ESRI\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":[\"Article\",\"BlogPosting\"],\"@id\":\"https:\/\/www.esri.com\/arcgis-blog\/products\/arcgis-desktop\/analytics\/field-mapping-and-python-scripting#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.esri.com\/arcgis-blog\/products\/arcgis-desktop\/analytics\/field-mapping-and-python-scripting\"},\"author\":{\"name\":\"Drew Flater\",\"@id\":\"https:\/\/www.esri.com\/arcgis-blog\/#\/schema\/person\/d417d5bffadc2ab9c4fcfeefb2fa87d6\"},\"headline\":\"Field mapping and Python scripting\",\"datePublished\":\"2012-08-31T06:14:38+00:00\",\"dateModified\":\"2018-12-18T19:21:39+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.esri.com\/arcgis-blog\/products\/arcgis-desktop\/analytics\/field-mapping-and-python-scripting\"},\"wordCount\":5,\"publisher\":{\"@id\":\"https:\/\/www.esri.com\/arcgis-blog\/#organization\"},\"keywords\":[\"geoprocessing\",\"python\",\"scripting\"],\"articleSection\":[\"Analytics\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.esri.com\/arcgis-blog\/products\/arcgis-desktop\/analytics\/field-mapping-and-python-scripting\",\"url\":\"https:\/\/www.esri.com\/arcgis-blog\/products\/arcgis-desktop\/analytics\/field-mapping-and-python-scripting\",\"name\":\"Field mapping and Python scripting\",\"isPartOf\":{\"@id\":\"https:\/\/www.esri.com\/arcgis-blog\/#website\"},\"datePublished\":\"2012-08-31T06:14:38+00:00\",\"dateModified\":\"2018-12-18T19:21:39+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.esri.com\/arcgis-blog\/products\/arcgis-desktop\/analytics\/field-mapping-and-python-scripting#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.esri.com\/arcgis-blog\/products\/arcgis-desktop\/analytics\/field-mapping-and-python-scripting\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.esri.com\/arcgis-blog\/products\/arcgis-desktop\/analytics\/field-mapping-and-python-scripting#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.esri.com\/arcgis-blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Field mapping and Python scripting\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.esri.com\/arcgis-blog\/#website\",\"url\":\"https:\/\/www.esri.com\/arcgis-blog\/\",\"name\":\"ArcGIS Blog\",\"description\":\"Get insider info from Esri product teams\",\"publisher\":{\"@id\":\"https:\/\/www.esri.com\/arcgis-blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.esri.com\/arcgis-blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.esri.com\/arcgis-blog\/#organization\",\"name\":\"Esri\",\"url\":\"https:\/\/www.esri.com\/arcgis-blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.esri.com\/arcgis-blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.esri.com\/arcgis-blog\/app\/uploads\/2018\/04\/Esri.png\",\"contentUrl\":\"https:\/\/www.esri.com\/arcgis-blog\/app\/uploads\/2018\/04\/Esri.png\",\"width\":400,\"height\":400,\"caption\":\"Esri\"},\"image\":{\"@id\":\"https:\/\/www.esri.com\/arcgis-blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/esrigis\/\",\"https:\/\/x.com\/ESRI\",\"https:\/\/www.linkedin.com\/company\/5311\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.esri.com\/arcgis-blog\/#\/schema\/person\/d417d5bffadc2ab9c4fcfeefb2fa87d6\",\"name\":\"Drew Flater\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.esri.com\/arcgis-blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/www.esri.com\/arcgis-blog\/app\/uploads\/2024\/05\/drew-465x465.jpg\",\"contentUrl\":\"https:\/\/www.esri.com\/arcgis-blog\/app\/uploads\/2024\/05\/drew-465x465.jpg\",\"caption\":\"Drew Flater\"},\"description\":\"Group Lead Product Engineer for the Geoprocessing development team at Esri, leading and contributing to projects across ArcGIS products with focus on ArcGIS Pro. Drew joined Esri in 2008, after completing his Masters in GIS at the University of Akron. Now he designs, codes, tests, and documents tools that can be used to understand and find meaning in raw geospatial data. He works to make Esri's geoprocessing and analytical capabilities easy to use and automate.\",\"url\":\"https:\/\/www.esri.com\/arcgis-blog\/author\/drewf\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Field mapping and Python scripting","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.esri.com\/arcgis-blog\/products\/arcgis-desktop\/analytics\/field-mapping-and-python-scripting","og_locale":"en_US","og_type":"article","og_title":"Field mapping and Python scripting","og_url":"https:\/\/www.esri.com\/arcgis-blog\/products\/arcgis-desktop\/analytics\/field-mapping-and-python-scripting","og_site_name":"ArcGIS Blog","article_publisher":"https:\/\/www.facebook.com\/esrigis\/","article_modified_time":"2018-12-18T19:21:39+00:00","twitter_card":"summary_large_image","twitter_site":"@ESRI","schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["Article","BlogPosting"],"@id":"https:\/\/www.esri.com\/arcgis-blog\/products\/arcgis-desktop\/analytics\/field-mapping-and-python-scripting#article","isPartOf":{"@id":"https:\/\/www.esri.com\/arcgis-blog\/products\/arcgis-desktop\/analytics\/field-mapping-and-python-scripting"},"author":{"name":"Drew Flater","@id":"https:\/\/www.esri.com\/arcgis-blog\/#\/schema\/person\/d417d5bffadc2ab9c4fcfeefb2fa87d6"},"headline":"Field mapping and Python scripting","datePublished":"2012-08-31T06:14:38+00:00","dateModified":"2018-12-18T19:21:39+00:00","mainEntityOfPage":{"@id":"https:\/\/www.esri.com\/arcgis-blog\/products\/arcgis-desktop\/analytics\/field-mapping-and-python-scripting"},"wordCount":5,"publisher":{"@id":"https:\/\/www.esri.com\/arcgis-blog\/#organization"},"keywords":["geoprocessing","python","scripting"],"articleSection":["Analytics"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.esri.com\/arcgis-blog\/products\/arcgis-desktop\/analytics\/field-mapping-and-python-scripting","url":"https:\/\/www.esri.com\/arcgis-blog\/products\/arcgis-desktop\/analytics\/field-mapping-and-python-scripting","name":"Field mapping and Python scripting","isPartOf":{"@id":"https:\/\/www.esri.com\/arcgis-blog\/#website"},"datePublished":"2012-08-31T06:14:38+00:00","dateModified":"2018-12-18T19:21:39+00:00","breadcrumb":{"@id":"https:\/\/www.esri.com\/arcgis-blog\/products\/arcgis-desktop\/analytics\/field-mapping-and-python-scripting#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.esri.com\/arcgis-blog\/products\/arcgis-desktop\/analytics\/field-mapping-and-python-scripting"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.esri.com\/arcgis-blog\/products\/arcgis-desktop\/analytics\/field-mapping-and-python-scripting#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.esri.com\/arcgis-blog\/"},{"@type":"ListItem","position":2,"name":"Field mapping and Python scripting"}]},{"@type":"WebSite","@id":"https:\/\/www.esri.com\/arcgis-blog\/#website","url":"https:\/\/www.esri.com\/arcgis-blog\/","name":"ArcGIS Blog","description":"Get insider info from Esri product teams","publisher":{"@id":"https:\/\/www.esri.com\/arcgis-blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.esri.com\/arcgis-blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.esri.com\/arcgis-blog\/#organization","name":"Esri","url":"https:\/\/www.esri.com\/arcgis-blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.esri.com\/arcgis-blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.esri.com\/arcgis-blog\/app\/uploads\/2018\/04\/Esri.png","contentUrl":"https:\/\/www.esri.com\/arcgis-blog\/app\/uploads\/2018\/04\/Esri.png","width":400,"height":400,"caption":"Esri"},"image":{"@id":"https:\/\/www.esri.com\/arcgis-blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/esrigis\/","https:\/\/x.com\/ESRI","https:\/\/www.linkedin.com\/company\/5311\/"]},{"@type":"Person","@id":"https:\/\/www.esri.com\/arcgis-blog\/#\/schema\/person\/d417d5bffadc2ab9c4fcfeefb2fa87d6","name":"Drew Flater","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.esri.com\/arcgis-blog\/#\/schema\/person\/image\/","url":"https:\/\/www.esri.com\/arcgis-blog\/app\/uploads\/2024\/05\/drew-465x465.jpg","contentUrl":"https:\/\/www.esri.com\/arcgis-blog\/app\/uploads\/2024\/05\/drew-465x465.jpg","caption":"Drew Flater"},"description":"Group Lead Product Engineer for the Geoprocessing development team at Esri, leading and contributing to projects across ArcGIS products with focus on ArcGIS Pro. Drew joined Esri in 2008, after completing his Masters in GIS at the University of Akron. Now he designs, codes, tests, and documents tools that can be used to understand and find meaning in raw geospatial data. He works to make Esri's geoprocessing and analytical capabilities easy to use and automate.","url":"https:\/\/www.esri.com\/arcgis-blog\/author\/drewf"}]}},"text_date":"August 30, 2012","author_name":"Drew Flater","author_page":"https:\/\/www.esri.com\/arcgis-blog\/author\/drewf","custom_image":"https:\/\/www.esri.com\/arcgis-blog\/app\/uploads\/2025\/08\/Newsroom-Keyart-Wide-1920-x-1080.jpg","primary_product":"ArcMap","tag_data":[{"term_id":24321,"name":"geoprocessing","slug":"geoprocessing","term_group":0,"term_taxonomy_id":24321,"taxonomy":"post_tag","description":"","parent":0,"count":129,"filter":"raw"},{"term_id":24341,"name":"python","slug":"python","term_group":0,"term_taxonomy_id":24341,"taxonomy":"post_tag","description":"","parent":0,"count":171,"filter":"raw"},{"term_id":24351,"name":"scripting","slug":"scripting","term_group":0,"term_taxonomy_id":24351,"taxonomy":"post_tag","description":"","parent":0,"count":15,"filter":"raw"}],"category_data":[{"term_id":23341,"name":"Analytics","slug":"analytics","term_group":0,"term_taxonomy_id":23341,"taxonomy":"category","description":"","parent":0,"count":1328,"filter":"raw"}],"product_data":[{"term_id":36991,"name":"ArcMap","slug":"arcgis-desktop","term_group":0,"term_taxonomy_id":36991,"taxonomy":"product","description":"","parent":36981,"count":325,"filter":"raw"}],"primary_product_link":"https:\/\/www.esri.com\/arcgis-blog\/?s=#&products=arcgis-desktop","_links":{"self":[{"href":"https:\/\/www.esri.com\/arcgis-blog\/wp-json\/wp\/v2\/blog\/181701","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.esri.com\/arcgis-blog\/wp-json\/wp\/v2\/blog"}],"about":[{"href":"https:\/\/www.esri.com\/arcgis-blog\/wp-json\/wp\/v2\/types\/blog"}],"author":[{"embeddable":true,"href":"https:\/\/www.esri.com\/arcgis-blog\/wp-json\/wp\/v2\/users\/5181"}],"replies":[{"embeddable":true,"href":"https:\/\/www.esri.com\/arcgis-blog\/wp-json\/wp\/v2\/comments?post=181701"}],"version-history":[{"count":0,"href":"https:\/\/www.esri.com\/arcgis-blog\/wp-json\/wp\/v2\/blog\/181701\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.esri.com\/arcgis-blog\/wp-json\/wp\/v2\/media?parent=181701"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.esri.com\/arcgis-blog\/wp-json\/wp\/v2\/categories?post=181701"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.esri.com\/arcgis-blog\/wp-json\/wp\/v2\/tags?post=181701"},{"taxonomy":"industry","embeddable":true,"href":"https:\/\/www.esri.com\/arcgis-blog\/wp-json\/wp\/v2\/industry?post=181701"},{"taxonomy":"product","embeddable":true,"href":"https:\/\/www.esri.com\/arcgis-blog\/wp-json\/wp\/v2\/product?post=181701"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}