delete multiple field using python

2503
2
Jump to solution
07-27-2013 10:41 PM
ElaineKuo
Occasional Contributor
Hello

System Windows Vista / ArcGIS 9.3

I have ten shape files.
Each of them has the fields starting with D in the attribute table. (D8739, D1983)
Some have 10 D fields, and other have about 20.

Now I want to delete the D fields using python.
However, no D field was deleted after running the code below.
Please kindly help to modify it and thank you in advance

##Script Name: calculate sum ##Description: calculate sum of merged range sizes of a taxonomy of migratory birds  ##Created By: Elaine Kuo ##Date: 26/05/2012    #Import standard library modules import arcgisscripting import os  #Create the Geoprocessor object gp = arcgisscripting.create(9.3)  #Set the workspace. gp.Workspace= "H:/temp_D/test_1" #gp.Workspace= "H:/temp_stage_2_3_polygon_limit_1984_no_R_O/P_Turdidae_22"  #Set the workspace. List all of the feature classes in the dataset outWorkspace= "H:/temp_D" #outWorkspace= "H:/temp_stage_2_3_polygon_limit_1984_no_R_O"  #Get a list of the featureclasses in the input folder fcs = gp.ListFeatureClasses()   # Loop through every item in the list that was just generated for fc in fcs:      # Break out the name, no path or extension, using the describe object.     desc = gp.describe(fc)     featureName = desc.name      # Make temporary featureclasses     gp.MakeFeatureLayer(fc,"lyr")          #   Get a list of the fields in the featureclass     fields = gp.ListFields("lyr")          # Loop through every item in the list that was just generated      for field in fields:          if field.name[1:4] == "D":             gp.deletefield ("lyr", field.name)            #Validate the new feature class name for the output workspace.     #OutFeatureClass = outWorkspace + os.sep + gp.ValidateTableName(fc,outWorkspace)      # copy feature     #gp.CopyFeatures("lyr", OutFeatureClass)      # clear memory of layers     gp.Delete("lyr")  gp.AddMessage(gp.GetMessages()) print gp.GetMessages()  
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
Luke_Pinner
MVP Regular Contributor
For your example field names D8739, D1983 field.name[1:4] is  873 and 198. You need to test field.name[0] == 'D'

        if field.name[0] == "D":             gp.deletefield ("lyr", field.name)      

View solution in original post

0 Kudos
2 Replies
Luke_Pinner
MVP Regular Contributor
For your example field names D8739, D1983 field.name[1:4] is  873 and 198. You need to test field.name[0] == 'D'

        if field.name[0] == "D":             gp.deletefield ("lyr", field.name)      
0 Kudos
ElaineKuo
Occasional Contributor
Thanks. It works well!
0 Kudos