I've got a featureclass (fc) that I want to copy, but only retaining a selected number of fields, let's say field 9, 11, and 12 from the total of 15 fields. I want to use arcpy and suspect that fieldmappings are somehow required for this. I currently have:
fm= arcpy.FieldMappings() fm.addtable(fc) sel= range(0,9)++range(13,16) for i in sel: fm.removeFieldMap(i)
However, this gives:
RuntimeError: FieldMappings: Error in removing a field map from field mappings for RemoveFieldMap
I can't quite see what I'm doing wrong? Perhaps it would be nicer to somehow select the fields I want and add these to a new fieldMapping object? All suggestions are welcome, thanks in advance!
Update. As suggested below, I now have the following:
inFields= ["fA", "fB", "fC"] inTab= 'testFc' fm= arcpy.fieldMappings() fieldMap= arcpy.FieldMap() for field in inFields: fieldMap.addInputField(inTab, field) fm.addFieldMap(fieldMap) arcpy.featureClassToFeatureClass_conversion(testFc, outDir, testOutFc, field_mapping= fm)
This works, a new featureclass is created. However, it contains only field fA. The python output reads:
Executing: FeatureClassToFeatureClass L:/GEOProjects/2Working.gdb/testFc :/GEOProjecten outDir testOutFc # "fA "fA" true true false 8 Double 0 0 ,First,#, L:/GEOProjects/2Working.gdb/testFc,fA,-1,-1, L:/GEOProjects/2Working.gdb/testFc,fB,-1,-1, L:/GEOProjects/2Working.gdb/testFc,fC,-1,-1" # Start Time: Thu Oct 24 10:21:42 2013 Succeeded at Thu Oct 24 10:21:58 2013 (Elapsed Time: 16.00 seconds)
It seems that ony fA is being copied to the new featureclass. Any suggestion as to why this can be?