I am trying to write a script to read a feature class, list all fields and keep only specic ones. I.e. drop all remaining columns.
I guess that pseudo code should be:
allFields
= create list of all fieldsfieldToKeep
= define list of fields to keep (manually)fieldsToDrop
= different items between list 1 and list2 = to remove- execute
arcpy.DeleteField_management(inFC, fieldsToDrop)
to remove uncecessary fields
Should be pretty simple, but I cannot figure out steps and conditioning in correct order?
# Input point data
inFC= "inFC"
# CHeck if field exist,if yes, delete the field
allFields = arcpy.ListFields(inFC)
fieldsToKeep = ["OBJECTID", "Shape", "Country", "manualChange"]
fieldsToDrop = [] # define new empty list
for field in allFields: # how to difference two lists?
if not field in fieldsToKeep:
fields.remove(field)
# Execute DeleteField
arcpy.DeleteField_management(inFC, fieldsToDrop)
fields_to_drop = [f.name for f in arcpy.ListFields(inFC) if f.type not in ('Geometry','OID') and f.name not in ["Country", "manualChange"]]