Select to view content in your preferred language

Looping problems! - Deleting fields

2741
2
02-18-2015 06:31 AM
MatthewRusso
New Contributor II

So I got my script to work but it is still giving me a problem.

When i run the script through command line it is printing out the elif clause about 10-15 times each before it starts to delete fields.  Anyone have any advice?

import arcpy

fieldList = ['ID','LAT','LON','AREA','PARIMETER','Hectares','CODE',
             'WEBCODE','DATE_','TIME_','SURVEYOR','TYPE','REV_DATE',
             'DATA_SRC','PATTERN','SWATH_DIST','VIEW_DIRC','CONDITIONS',
             'CONFIDENCE','RPT_YR','SURVEY_ID1','SURVEY_ID2','SURVEY_ID3',
             'DMG_TYPE1','DMG_TYPE2','DMG_TYPE3','SEVERITY1','SEVERITY2',
             'SEVERITY3','PATTERN1','PATTERN2','PATTERN3','TPA1', 'TPA2',
             'TPA3','NO_TREES1','NO_TREES2','NO_TREES3','NOTES','NOTE',
             'DAMAGE94_','DAMAGE94_I','STATUS','MODIFIIER','ACRES',
             'PESTNUM','MODNUM','GRP1','GRP2','GRP3','SURVEY_YR','RPT_RGN',
             'R295_DMG_I','R295_DMG_','R2_PC3','R2_PC2','R2_PC1','R201_DMG_',
             'R201_DMG_I','AREA_METER', 'SOURCETHM','PCT_MORT1','PCT_MORT2',
             'PCT_MORT3']
             
arcpy.env.workspace = r'H:\GIS DATA\Data\Pine Beetle\Beetle\Test'
fcs = arcpy.ListFeatureClasses()

for fc in fcs:
    for i in fieldList:
        x = arcpy.ListFields(fc)
        for a in x:
            if i == a.name:
                arcpy.DeleteField_management(fc, i)
                print i + ' was deleted from ' + fc
            elif i != a.name:
                print i
0 Kudos
2 Replies
JakeSkinner
Esri Esteemed Contributor

Hi Matthew,

That should be correct.  For each field in your list, it will compare it against each field in each feature class.  It does not look at all of the fields in the feature class at once and do a comparison.  For example, the first value in the list, ID, is compared against OBJECTID, SHAPE.AREA, SHAPE.LENGTH, etc.

0 Kudos
MatthewRusso
New Contributor II

Maybe I should just remove the print for the elif statement, it just fills my command line up with excess.

Thanks for the response.

0 Kudos