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']

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Oct 4, 2016 at 15:25
1
  • 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. Commented Oct 4, 2016 at 21:22

1 Answer 1

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)
answered Oct 4, 2016 at 15:33
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.