I am trying to use the Featureclass to Featureclass conversion tool to make a copy of an existing layer (in a geodatabase). My primary motive is to copy over only a few (2 out of 18) fields to the new feature class. I have put a simple script to do this, but this copies all the fields even tough I specify the field names that should be copied.
Can someone please let me know what am i doing wrong.
I want the output feature class (Test) to have two fields A and B.
# Import arcpy module
import arcpy
# Local variables:
Input = "C:\\Work\\Projects\0円a_IL_Naperville\\Segmenter_v8\1円 00001 IL Naperville 2013.gdb\\Streets"
Out_location = "C:\\Work\\Projects\0円a_IL_Naperville\\Segmenter_v8\1円 00001 IL Naperville 2013.gdb"
keep = ['ST_NAME','FUNC_CLASS','Shape_Length','OBJECTID','Shape','A']
arcpy.FeatureClassToFeatureClass_conversion(Input,Out_location, "Test",field_mapping=keep)
2 Answers 2
If you look at ESRI'S field map documentation, it seems the good way to specify fields is with a arcpy.FieldMap() object. The second example on the documentation seems to fit your needs :
import arcpy
# Set the workspace
arcpy.env.workspace = 'c:/base/data.gdb'
in_file = 'AccidentData'
out_file = 'AverageAccidents'
# Create the necessary FieldMap and FieldMappings objects
fm = arcpy.FieldMap()
fm1 = arcpy.FieldMap()
fms = arcpy.FieldMappings()
# Each field with accident data begins with 'Yr' (from Yr2007 to Yr2012).
# The next step loops through each of the fields beginning with 'Yr',
# and adds them to the FieldMap Object
for field in arcpy.ListFields(in_file, 'Yr*'):
fm.addInputField(in_file, field.name)
# Set the merge rule to find the mean value of all fields in the
# FieldMap object
fm.mergeRule = 'Mean'
# Set properties of the output name.
f_name = fm.outputField
f_name.name = 'AvgAccidents'
f_name.aliasName = 'AvgAccidents'
fm.outputField = f_name
# Add the intersection field to the second FieldMap object
fm1.addInputField(in_file, "Intersection")
# Add both FieldMaps to the FieldMappings Object
fms.addFieldMap(fm)
fms.addFieldMap(fm1)
# Create the output feature class, using the FieldMappings object
arcpy.FeatureClassToFeatureClass_conversion(
in_file, arcpy.env.workspace, out_file, field_mapping=fms)
-
Ok so I did not go the route you suggested, but I did try something else. See edited code above. This gives me an error that I cannot figure out. What am i doing wrong. BTW i am new to python so bear with my rather novice questions.Abhi– Abhi2014年06月04日 23:13:41 +00:00Commented Jun 4, 2014 at 23:13
-
I got it tow work. Cannot post my script now because its less than 8 hrs. but i will laterAbhi– Abhi2014年06月05日 00:27:21 +00:00Commented Jun 5, 2014 at 0:27
OK so the following script worked for me. I ended up using field mapping as suggested by Goldorak84.
# Import arcpy module
import arcpy
arcpy.env.workspace = "C:\\Work\\Projects\0円a_IL_Naperville\\Segmenter_v8\\x_1 00001 IL Naperville 2013.gdb"
# Local variables:
Streets = "C:\\Work\\Projects\0円a_IL_Naperville\\Segmenter_v8\\x_1 00001 IL Naperville 2013.gdb\\Streets"
out = "C:\\Work\\Projects\0円a_IL_Naperville\\Segmenter_v8\\x_1 00001 IL Naperville 2013.gdb"
keep = ['ST_NAME','FUNC_CLASS','A']
fieldMappings = arcpy.FieldMappings()
fieldMappings.addTable(Streets)
for field in fieldMappings.fields:
if field.name not in keep:
fieldMappings.removeFieldMap(fieldMappings.findFieldMapIndex(field.name))
arcpy.FeatureClassToFeatureClass_conversion(Streets, out, "Test1","",fieldMappings ,"")