Delete multiple fields

643
4
Jump to solution
10-22-2012 07:39 AM
DanielAbera
New Contributor III
Hi Guys:

I am trying to use a pyhton script to delete multiple fields which are not in the list, but its not working. Any idea how to make is working. The code is as follows. Thanks.

import arcgisscripting, sys gp = arcgisscripting.create()    # set variables inputFc = sys.argv[1]  fdList = ['FID', 'Shape', 'MER', 'RGE', 'TWP', 'SEC', 'QS', 'Hectares']  fields = gp.ListFields(inputFc)   for field in fields:   if field.name not in fdList:        gp.deletefield_management(inputFc)  
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
MathewCoyle
Frequent Contributor
Here is how I delete fields in 10.0. You have pretty much everything done already for 9.3.1.

dropFields = list() fieldList = arcpy.ListFields(block_out) keep_list = ["OBJECTID", "OID", "Geometry", "O_OID", "SOURCEID"] for f in fieldList:     if f.name not in keep_list and f.type not in keep_list:         dropFields.append(f.name) arcpy.DeleteField_management(block_out, dropFields)

View solution in original post

0 Kudos
4 Replies
MathewCoyle
Frequent Contributor
What version of ArcGIS are you running?

You also need to specify what field you want to delete.

gp.deletefield_management(inputFc, field.name)
0 Kudos
DanielAbera
New Contributor III
I am using 9.3.1, and I want to delete all but those specified in the list. I know you can delete multiple fields by hard coding the field names. But, I just want to something like to delete all except the field names specified.  I can you ArcGIS 10.0 too.

Thanks,
Dan
0 Kudos
MathewCoyle
Frequent Contributor
Here is how I delete fields in 10.0. You have pretty much everything done already for 9.3.1.

dropFields = list() fieldList = arcpy.ListFields(block_out) keep_list = ["OBJECTID", "OID", "Geometry", "O_OID", "SOURCEID"] for f in fieldList:     if f.name not in keep_list and f.type not in keep_list:         dropFields.append(f.name) arcpy.DeleteField_management(block_out, dropFields)
0 Kudos
DanielAbera
New Contributor III
This is exactly what I was looking. Its works like charm.
Thanks a lot Matt !!!
0 Kudos