DeleteField_management (delete all except 3 fields)

4469
9
09-14-2012 12:04 PM
BlakeMcMahon
New Contributor
I have a shape that has twenty fields or more.  I want to delete all fields except three using python code.  I thought of using DeleteField_management, but it looks like I have to know the name of the fields I want to delete.  Unfortunately, in my case I only know the name of the fields I want to keep.  Is there a way to delete all the fields except the 3 I want to keep which are ("UGISID", "LBL_X", "LBL_Y")?  or is there a way to export out the shape with only the 3 fields and then deleting the one with 20?

Again,  the issue is: Everytime I use this script, a new shape will be used and all I need are the three fields and there is no way to know what fields need to get deleted, only that I need ("UGISID", "LBL_X", "LBL_Y") when it comes out the other end.
Tags (2)
0 Kudos
9 Replies
MichaelVolz
Esteemed Contributor
Use the ListFields method to get a list of fields.

Then loop through these fields and add a if-then statement if the field name is equal to any of the 3 fields you want to keep.  If not then delete the field.
0 Kudos
KenBuja
MVP Honored Contributor
You can make a minor revision to the example 3 code found in the Delete Field help. Where it adds the fields to be deleted, put in an additional if statement to check if field is not equal to "UGISID", "LBL_X", or "LBL_Y".

# For each field in the object list, add the field name to the
#  name list.  If the field is required, exclude it, to prevent errors

exclude = ["UGISID", "LBL_X", "LBL_Y"]

for field in fieldObjList:
     if not field.required:
          if not field.name in exclude:
               fieldNameList.append(field.name)
0 Kudos
MarkHoyland
Occasional Contributor II
If your objective is to export to a new shape file, you could use FeatureClassToFeatureClass.
FeatureClassToFeatureClass allows for Field Mappings, so you can specify which fields to keep.

inputShapeFile = "C:/Temp/yourInput.shp"
outputLocation = "C:/Temp"
outputShapeFileName = "yourExport.shp"

# List of the fields to keep.
outputFields = ["UGISID", "LBL_X", "LBL_Y"]

fieldMappings = arcpy.FieldMappings()

# create the field mappings from the outputFields list.
# only fields in the list will be included in the exported shape file.
for field in outputFields:
    fieldMap = arcpy.FieldMap()
    fieldMap.addInputField(inputShapeFile, field)
    fieldMappings.addFieldMap(fieldMap)

# Use FeatureClassToFeatureClass and apply the fieldMappings
arcpy.FeatureClassToFeatureClass_conversion(inputShapeFile,
                                            outputLocation,
                                            outputShapeFileName,
                                            "",
                                            fieldMappings,
                                            "")
0 Kudos
DougHaller1
New Contributor III

Hi Mark,

Thanks for the above. 

Couple of questions (assume I know only a little Python, thanks)

  • How can you do this task if you only know approximations of the field names? I am using public domain data and the authors of the shapefiles use different field names to describe the same thing. For example, the field name for a location might be "trailhead" or "trail_head". I assume I need to write a script that uses a "like" statement such as like "trailhead" but I do not know the exact syntax for like.  Is it "==" or with a "*" or is it more complex?
  • Would you process the data as a shapefile or convert it to a spreadsheet and then keep only the wanted data?

Thanks,

Doug

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

There are so many different ways of tackling this issue, i.e., the "SQL LIKE with Python strings," that I encourage you to hit up Google or some other search engine.  The question you have isn't really an ArcPy question, it is a general Python question, that has lots of answers floating around the web.

0 Kudos
DougHaller1
New Contributor III

Thanks Josh. And thanks for keeping an eye on old threads.

I agree on all points.

I'm in the "gathering information" step before writing any code.  I will take a look at other sources.

I need to remove unneeded data from multiple shapefiles saved in a .gdb. 

Since the data originates as .shp files, I thought it would be best to work with arcpy and tap the GIS community.

Cheers,

Doug

0 Kudos
DougHaller1
New Contributor III

Josh,

Is there a way to keep the file name but add a suffix. For example, if my file is named XYZ.shp and I want to remove some fields and name the new file, XYZsubset.shp?  I want to use the inputShapeFile name as part of the outputShapefile name. Thanks

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

The following is an example of how you can append a suffix to a file name:

>>> orig_name = "XYZ.shp"
>>> suffix = "subset"
>>> p, x = orig_name.rsplit(".", 1)
>>> new_name = "{}{}.{}".format(f, suffix, x)
>>> new_name
'XYZsubset.shp'
>>> 
0 Kudos
DougHaller1
New Contributor III

Thanks Josh!

0 Kudos