Select to view content in your preferred language

Export Table with selected fields

04-18-2023 07:30 AM
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.




0 Kudos
2 Replies
by Anonymous User
Not applicable

Use the field mappings:


fieldmappings = arcpy.FieldMappings()

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

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



0 Kudos
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