delete field based on CXXXX

1237
4
Jump to solution
06-27-2013 04:25 PM
ElaineKuo
Occasional Contributor
System: Windows Vista and ArcGIS 9.3

Hello,

I have 100 shapefiles called geocXXXX.shp (geoc0782, geoc9838, and geoc6728 etc)

In each shapefile, there is a field call CXXXX (C0782, C9838, and C6728 etc).
The field property is string, length =6.

Also, there are other fields called CXXXXMS and CXXXXMW (C0782MS and C0782MW).
The field property is long integer, length =9.

I would like to delete the fields of CXXXX, CXXXXMS, and CXXXXMW at the same time.
However, the following code did not work.
Please kindly advise how to modify it and thank you.

#Import standard library modules import arcgisscripting import os  #Create the Geoprocessor object gp = arcgisscripting.create(9.3)  #Set the input workspace #GP.workspace = sys.argv[1] #Set the workspace. gp.Workspace= "H:/temp_D/test"  #Set the output workspace #outWorkspace = sys.argv[2] #Set the workspace. List all of the feature classes in the dataset outWorkspace= "H:/temp_D"   #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:       # delete the field CXXXX     gp.deletefield (fc, "C*")      gp.AddMessage(gp.GetMessages()) print gp.GetMessages()
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
MarkBryant
New Contributor III
Here is a snippet of code that I use to build a list of fields to drop at 10.

    # Create an empty list that will be populated with field names             dropFieldNameList = []     # Use ListFields to get a list of field objects     fieldList = arcpy.ListFields(inTable)          for field in fieldList:         #print field.name.upper()         if not field.required:             if field.name.upper().startswith('C'):                 dropFieldNameList.append(field.name)      # Execute DeleteField to delete all fields in the field list.      arcpy.DeleteField_management(inTable, dropFieldNameList) 


The test used to find the fields
 if field.name.upper().startswith('C'):

will drop all fields starting with C.
If there are other fields starting with C that you want to keep, then you need to adjust the test.

Mark
Mark.

View solution in original post

0 Kudos
4 Replies
RhettZufelt
MVP Frequent Contributor
#Import standard library modules
import arcgisscripting
import os

#Create the Geoprocessor object
gp = arcgisscripting.create(9.3)

gp.toolbox = "management"

#Set the input workspace
#GP.workspace = sys.argv[1]
#Set the workspace.
gp.workspace= "H:/temp_D/test"         

#Set the output workspace
#outWorkspace = sys.argv[2]
#Set the workspace. List all of the feature classes in the dataset

#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:
    fields = gp.ListFields(fc,"C*", "String")
    for field in fields:
        # if not field.endswith("D"): 
          # delete the field CXXXX
            gp.deletefield (fc, field)   
    del field
    del fields

gp.AddMessage(gp.GetMessages())
print gp.GetMessages()



Don't believe deletefield allows wildcards. This way I'm looping through the fields in the current FC and adding any that start with C to a list. Then, iterate through the list of Cfields and delete.

I think that should do it for you. I don't have 9.3 anymore to test with.

Also, this will delete the CXXXXD fields if they still exist also (if these are the same datasets you are adding to in your other post)... Uncomment out the section in red if you don't want to delete the fields that end with D as well.
Of course, you could probably just add the deletefield option to your other script and delete the C* field right after the calculate (while the variable is still set to it).


R_
0 Kudos
MarkBryant
New Contributor III
Here is a snippet of code that I use to build a list of fields to drop at 10.

    # Create an empty list that will be populated with field names             dropFieldNameList = []     # Use ListFields to get a list of field objects     fieldList = arcpy.ListFields(inTable)          for field in fieldList:         #print field.name.upper()         if not field.required:             if field.name.upper().startswith('C'):                 dropFieldNameList.append(field.name)      # Execute DeleteField to delete all fields in the field list.      arcpy.DeleteField_management(inTable, dropFieldNameList) 


The test used to find the fields
 if field.name.upper().startswith('C'):

will drop all fields starting with C.
If there are other fields starting with C that you want to keep, then you need to adjust the test.

Mark
Mark.
0 Kudos
ElaineKuo
Occasional Contributor
Thanks to Harles's idea, I modified the code to delete multiple fields starting with C, using ArcGIS 9.3


#Import standard library modules
import arcgisscripting
import os

#Create the Geoprocessor object
gp = arcgisscripting.create(9.3)

#Set the input workspace
#GP.workspace = sys.argv[1]
#Set the workspace.
gp.Workspace= "H:/temp_D/test"

#Set the output workspace
#outWorkspace = sys.argv[2]
#Set the workspace. List all of the feature classes in the dataset
outWorkspace= "H:/temp_D"


#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:

    # Get the numeric code from the shapefile name
    numCode = fc[4:8]
    
    # Build the field name
    fieldName1 = "C"+numCode+"MS"  
    fieldName2 = "C"+numCode+"MW"

    # delete the field CXXXX
    gp.deletefield (fc, fieldName1)
    gp.deletefield (fc, fieldName2)

gp.AddMessage(gp.GetMessages())
print gp.GetMessages()
0 Kudos
curtvprice
MVP Esteemed Contributor
    # Build the field name
    fieldName1 = "C"+numCode+"MS"  
    fieldName2 = "C"+numCode+"MW"

    # delete the field CXXXX
    gp.deletefield (fc, fieldName1)
    gp.deletefield (fc, fieldName2)


Hi Elaine,

The DeleteField tool can take a list of field names either as a ";"-delimited string or a Python list:

gp.DeleteField_management(fc, [fieldName1, fieldName2])  
gp.DeleteField_management(fc, "{0};{1}".format(fieldName1, fieldName2))


By the way, I'd get in the habit of capitalizing tool names so you don't have to break the habit in 10.x when you get there.
0 Kudos