0

I have several duplicate fields with under score characters in a feature class that I would like to delete. How can this be accomplished using ArcPy? My code below lists the fields, what steps should be executed next to delete anything in field_names?

fc= "featureclass"
field_names = [f.name for f in arcpy.ListFields(fc)]
print field_names

Output:

[u'OBJECTID', u'JOBID', u'MOBILEID', u'COMPANY', u'ADDRESS', u'CITY', u'ZIPCODE', u'WORKSTATUS', u'WTBDTDATE', u'WTBDTTIME', u'WORKTYPE', u'LATTITUDE', u'LONGITUDE', u'DAYS_OVERDUE', u'OVERDUE_STATUS', u'SHAPE', u'STATUS', u'ZONEID', u'PRIORITY', u'ORIGPRIORITY', u'NEXTJOBID', u'MOBILE', u'DESCRIPTION', u'REGION', u'DISTRICT', u'COMPANY_1', u'SHAPE_LEN_1', u'MOBILE_1', u'DESCRIPTION_1', u'REGION_1', u'DISTRICT_1', u'COMPANY_12', u'SHAPE_LEN_12', u'WORSTOFFENDER', u'SHAPE.AREA', u'SHAPE.LEN']

  • 1
    Did you try looking in the help for a tool using "delete field" as a search string? Once found, all tools have code examples that I think you should test and incorporate into your own efforts before posting your code snippet here. – PolyGeo Oct 4 '16 at 21:22
10

Here's a pythonic way of removing the items containing underscores in a list.

field_names[:] = [x for x in field_names if not "_" in x]

Applying that for deletefield_management, which requires the fields to be deleted, and doing some work to turn the fields into strings:

field_list = arcpy.ListFields(fc)

field_list[:] = [x for x in field_list if "_" in x.name]

fields_to_delete  = []

for item in field_list:
     fields_to_delete.append(item.name)

arcpy.DeleteField_management(fc, fields_to_delete)
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.