Select to view content in your preferred language

Export Table with selected fields

247
2
04-18-2023 07:30 AM
Williams_Gregory
New Contributor II

I just need a simple sript that will copy a feature layer and convert it to a shapefile with just a four of the original field names.  I know how to do this manually but I need python script so it can automatically every morning.  I tried arcpy.TableToTable_conversion and FeatureClassTo FeatureClass but can't seem to figure it out how to select only those field name that are required for my project.

 

Thanks,

 

0 Kudos
2 Replies
by Anonymous User
Not applicable

Use the field mappings:

 

fieldmappings = arcpy.FieldMappings()
fieldmappings.addTable(fc)

keepfields = ['F1','F2','F3','F4']

# Removing unwanted fields
for field in fieldmappings.fields:
    if all([not field.required, field.name not in keepfields]):
        fieldmappings.removeFieldMap(fieldmappings.findFieldMapIndex(field.name))
        
arcpy.FeatureClassToFeatureClass_conversion(fc, 'outdir', 'outname', '', fieldmappings)

 

 

0 Kudos
AlfredBaldenweck
MVP Regular Contributor

If it's always the same four fields, the easiest way to do it is to do it manually once, then open up your geoprocessing history and copy as a python command/send to the python window and copypaste into your project.

0 Kudos