Field Mapping in ArcPy gives Error in adding input field to field map?

المشرف العام

Administrator
طاقم الإدارة
I have a process in which I am joining a DBF table to multiple shapefiles to create a new output of some selected fields which result from the join. I am new to fieldMapping and need help figuring out how to do this. Below is what I have so far.

In summary, I have 1 field from the shapefiles called "AREAHA" and 5 fields in the DBF that I need to make it to the output. They are joined by UKEY. I am using Feature Class to Feature Class as my method of exporting my feature.

I have attempted many versions of what you see here. I figured that seeing as though I need all fields from the DBF, i would make that my field map and just add the one field that I was missing (AREAHA). This has not worked. "Error in adding input field to field map" is the current error.

In the end all I need is this: arcpy.FeatureClassToFeatureClass_conversion(FCLayer, OutputLOC, "TSA_PE_" + fc, "",["F_DEL", "UKEY", "AREAHA","BLOCKS","POLY_NUM"])

But it is not that simple it appears... Doing a join and a FOR loop, i think, complicates it.

import arcpy, os, sysfrom arcpy import envenv.OverWriteOutput = TrueFieldMappings = arcpy.FieldMappings()FieldMap = arcpy.FieldMap()env.workspace = "\\\\silver\\clients\\Projects\\P747\\3_Landbase\\LB1\\lb_20141122\\"OutputLOC = "\\\\silver\\clients\\Projects\\P747\\3_Landbase\\LB1\\Eliminate\\"Table = "\\\\silver\\clients\\Projects\\P747\\3_Landbase\\LB1\\Eliminate\\El_20140922\\Export_Output.dbf"FCLayer = "\\\\silver\\clients\\Projects\\P747\\3_Landbase\\LB1\\Eliminate\\FCLayer.lyr"TableLayer = "\\\\silver\\clients\\Projects\\P747\\3_Landbase\\LB1\\Eliminate\\TableLayer.lyr"att = "AREAHA"arcpy.MakeTableView_management(Table, TableLayer)fieldMap.addInputField(TableLayer, att)fieldMappings.addFieldMap(FieldMap)for fc in arcpy.ListFeatureClasses(): arcpy.MakeFeatureLayer_management(fc, FCLayer) arcpy.AddJoin_management(FCLayer, "UKey", TableLayer, "UKEY", "KEEP_ALL") arcpy.FeatureClassToFeatureClass_conversion(FCLayer, OutputLOC, "TSA_PE_" + fc, "#", FieldMappings)So I have the code working now based on the advice from below. However, the output has just the columns and all blank rows; as if no join was performed. I am researching this problem but have yet to find a good solution. Any help in this would be appreciated.

import arcpy, os, sysfrom arcpy import envenv.OverWriteOutput = TrueFieldMappings = arcpy.FieldMappings()FieldMap1 = arcpy.FieldMap()FieldMap2 = arcpy.FieldMap()FieldMap3 = arcpy.FieldMap()FieldMap4 = arcpy.FieldMap()FieldMap5 = arcpy.FieldMap()env.workspace = "\\\\silver\\clients\\Projects\\P747\\3_Landbase\\LB1\\lb_20141122\\"OutputLOC = "\\\\silver\\clients\\Projects\\P747\\3_Landbase\\LB1\\Eliminate\\"Table = "\\\\silver\\clients\\Projects\\P747\\3_Landbase\\LB1\\Eliminate\\El_20140922\\Export_Output.dbf"FCLayer = "\\\\silver\\clients\\Projects\\P747\\3_Landbase\\LB1\\Eliminate\\FCLayer.lyr"TableLayer = "\\\\silver\\clients\\Projects\\P747\\3_Landbase\\LB1\\Eliminate\\TableLayer.lyr"if arcpy.Exists(FCLayer): arcpy.Delete_management(FCLayer)if arcpy.Exists(TableLayer): arcpy.Delete_management(TableLayer)arcpy.MakeTableView_management(Table, TableLayer)print '---1---'FieldMap1.addInputField(TableLayer, 'UKEY')FieldMap2.addInputField(TableLayer, 'BLOCKS')FieldMap3.addInputField(TableLayer, 'POLY_NUM')FieldMap4.addInputField(TableLayer, 'F_DEL')print '---2---'for fc in arcpy.ListFeatureClasses(): print ' huh' arcpy.MakeFeatureLayer_management(fc, FCLayer) print '---3---' FieldMap5.addInputField(FCLayer, 'AREAHA') print '---4---' FieldMappings.addFieldMap(FieldMap1) FieldMappings.addFieldMap(FieldMap2) FieldMappings.addFieldMap(FieldMap3) FieldMappings.addFieldMap(FieldMap4) FieldMappings.addFieldMap(FieldMap5) print '---6---' arcpy.AddJoin_management(FCLayer, "UKey", TableLayer, "UKEY", "KEEP_ALL") print '---7---' if arcpy.Exists(OutputLOC + "TSA_PEW_" + fc): arcpy.Delete_management(OutputLOC + "TSA_PEW_" + fc) print '---8---' arcpy.FeatureClassToFeatureClass_conversion(FCLayer, OutputLOC, "TSA_PEW_" + fc.rstrip(".shp"), "#", FieldMappings) arcpy.Delete_management(FCLayer) #arcpy.Delete_management(TableLayer) break

أكثر...
 
أعلى