AnsweredAssumed Answered

Python - Remove rows in Shapefile Based on Criteria in a Field

Question asked by pjordan@drcog.org on Mar 15, 2018
Latest reply on Mar 15, 2018 by pjordan@drcog.org

  I have a sample script for a test shapefile (which I eventually will apply to an actual shapefile, but for simplicity I'll just use this) that I want to run outside of Arc as a standalone script.

 

Shape file name: TEST.shp

Field: fclass

Attributes to Purge (in fclass): "A", "B", "C"

 

  So with this script, I want all records that have an fclass containing A, B or C to be deleted and leaving everything else.

 

Here's where I have an issue.  I can get this script to work with just one of the attributes, but not a combination of attributes (Sorry I can't seem to find a script code option to format this better):

 

 

print "Importing..."


import arcpy, os

 

shp = r'U:\TEST\TEST.shp'


list = ['1', '2', '3']

 

print "Purging unnecessary fclass values..."

 

with arcpy.da.UpdateCursor(shp, "fclass") as cursor:


for row in cursor:

     #if row[0] == "1": -- if I use just this, it works and deletes all rows with "1"

     if row[0] == [list]:
     cursor.deleteRow()

 

print "Completed"

 

Using the if row[0] == [list]doesn't do anything and neither does if row[0] == ["1","2","3"]I also don't want to use multiple OR statements if I can help it, similar to this (though the syntax for python OR statements might not look like this)

   if row[0] == '1': OR

   if row[0] == '2': OR

   if row[0] == '3':

 

  I've also played around using the different quotes as well in the script switching between the " and the ' without any success as well, thinking that was maybe the issue.

 

  I am not getting any errors at all, but it's just not deleting the records at all either...

 

Thanks

 

 

Outcomes