Merge_management and fieldmappings problem

1762
4
02-17-2016 08:06 AM
ChrisMatthews
New Contributor III

I am using arcpy.Merge_management to combine 4 CSV files into SDE. I attempting to use fieldmappings to rename the field headers, and this is causing it to crash. I've not used fieldmapping with more than two datasets simaltaneously before...

The error is trapped by the try loop, but is returning no information. Any help would be appreciated.

try:

    if booContinue == True:

        fieldmappings = arcpy.FieldMappings()

        fieldmap1 = arcpy.FieldMap()

        fieldmappings.addTable(rg_Layer)

        fieldmappings.addTable(ox_Layer)

        fieldmappings.addTable(sn_Layer)

        fieldmappings.addTable(sp_Layer)

        fieldmap1.addInputField(rg_Layer,"field1")

        fieldmap1.addInputField(ox_Layer,"field1")

        fieldmap1.addInputField(sn_Layer,"field1")

        fieldmap1.addInputField(sp_Layer,"field1")

        fieldout1 = fieldmap1.outputField

        fieldout1.name = "PCODE"

        fieldmap1.outputField = fieldout1

        fieldmappings.addFieldMap(fieldmap1)

       

        arcpy.Merge_management([rg_Layer,ox_Layer,sn_Layer,sp_Layer], pc_layer, fieldMappings)

        msg = msg + "\n" + outname + " created"

except:

    msg = msg + "\n" + "error creating " + outname + " - " + "\n" + arcpy.GetMessages(2)

    booContinue = False

0 Kudos
4 Replies
DanPatterson_Retired
MVP Esteemed Contributor

Chris  does fieldmapping work manually with csv files?  The help files only makes reference to featureclasses/shapefiles in the help topic but doesn't indicate what it can be used for

FieldMappings—Help | ArcGIS for Desktop

ChrisMatthews
New Contributor III

I've managed to get the Field_Mapping working, but as a massively complex (hard to read) parameter to the Merge_management tool - see below, so it does seem that it is possible to use field mapping with CSVs.

arcpy.Merge_management("rg_Layer;ox_Layer;sn_Layer;sp_Layer", GISLive_GISADMIN_AA_TEST_70, "Postcode \"Postcode\" true true false 255 Text 0 0 ,First,#,rg_Layer,Field1,-1,-1,ox_Layer,Field1,-1,-1,sn_Layer,Field1,-1,-1,sp_Layer,Field1,-1,-1;Quality \"Quality\" true true false 4 Long 0 0 ,First,#,rg_Layer,Field2,-1,-1,ox_Layer,Field2,-1,-1,sn_Layer,Field2,-1,-1,sp_Layer,Field2,-1,-1;POBox \"POBox\" true true false 255 Text 0 0 ,First,#,rg_Layer,Field3,-1,-1,ox_Layer,Field3,-1,-1,sn_Layer,Field3,-1,-1,sp_Layer,Field3,-1,-1;TotalDPS \"TotalDPS\" true true false 4 Long 0 0 ,First,#,rg_Layer,Field4,-1,-1,ox_Layer,Field4,-1,-1,sn_Layer,Field4,-1,-1,sp_Layer,Field4,-1,-1;ResDPS \"ResDPS\" true true false 4 Long 0 0 ,First,#,rg_Layer,Field6,-1,-1,ox_Layer,Field6,-1,-1,sn_Layer,Field6,-1,-1,sp_Layer,Field6,-1,-1;ComDPS \"ComDPS\" true true false 4 Long 0 0 ,First,#,rg_Layer,Field7,-1,-1,ox_Layer,Field7,-1,-1,sn_Layer,Field7,-1,-1,sp_Layer,Field7,-1,-1;POBoxDPS \"POBoxDPS\" true true false 4 Long 0 0 ,First,#,rg_Layer,Field8,-1,-1,ox_Layer,Field8,-1,-1,sn_Layer,Field8,-1,-1,sp_Layer,Field8,-1,-1;Matched \"Matched\" true true false 4 Long 0 0 ,First,#,rg_Layer,Field9,-1,-1,ox_Layer,Field9,-1,-1,sn_Layer,Field9,-1,-1,sp_Layer,Field9,-1,-1;Unmatched \"Unmatched\" true true false 4 Long 0 0 ,First,#,rg_Layer,Field10,-1,-1,ox_Layer,Field10,-1,-1,sn_Layer,Field10,-1,-1,sp_Layer,Field10,-1,-1;XCoord \"XCoord\" true true false 4 Long 0 0 ,First,#,rg_Layer,Field11,-1,-1,ox_Layer,Field11,-1,-1,sn_Layer,Field11,-1,-1,sp_Layer,Field11,-1,-1;YCoord \"YCoord\" true true false 4 Long 0 0 ,First,#,rg_Layer,Field12,-1,-1,ox_Layer,Field12,-1,-1,sn_Layer,Field12,-1,-1,sp_Layer,Field12,-1,-1;PCType \"PCType\" true true false 255 Text 0 0 ,First,#,rg_Layer,Field19,-1,-1,ox_Layer,Field19,-1,-1,sn_Layer,Field19,-1,-1,sp_Layer,Field19,-1,-1")

0 Kudos
XanderBakker
Esri Esteemed Contributor

Wouldn't it be easier to use a csv reader and order fields (indexes to fields) manually. Just seems that using FieldMappings (if it works on CSV files as Dan mentions) makes it unnecessarily complex. 

ChrisMatthews
New Contributor III

It is ironic that it ends up more complex using Field_Mapping, I wanted to use it to keep things simply.

0 Kudos