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

2465
5
Jump to solution
03-15-2018 08:06 AM
PeteJordan
Occasional Contributor III

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

    :

     cursor.deleteRow()

print "Completed"

 

Using the if row[0] ==

    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

0 Kudos
1 Solution

Accepted Solutions
MitchHolley1
MVP Regular Contributor

First - I had a syntax error in my original reply.  It has been edited, please try that one. 

Second - Click the three eellipses when writing the response -> click 'More' -> 'Syntax Highlighter'

View solution in original post

5 Replies
JoshuaBixby
MVP Esteemed Contributor

The Python syntax you are looking for is item in list.  That said, a more database/set-based approach would be to Make Feature Layer, Select Layer By Attribute, and then Delete Features.

MitchHolley1
MVP Regular Contributor

You should use the in python function to see if the value of the row you're iterating over is found in the list.  See below for an example.  

Note:  make sure to convert the values to a string before comparing (row 10), since the values in the deletedValues list are strings. 

import arcpy

dataset = r'path\to\dataset.shp'

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

cursor = arcpy.da.UpdateCursor(dataset, ['FIELD'])

for count, d in enumerate(cursor):
    if str(d[0]) in deleteValues:
        cursor.deleteRow()
del cursor


print '{0} rows were deleted.'.format(count)
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
PeteJordan
Occasional Contributor III

 I get a "Cursor" not defined error with the cursor.deleteRow(), plus how do you convert the values to a string in row 10?  I'm not really familiar with Python.  Also what's the format code to make the script show up with the row numbers like you had?

import arcpy

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

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

deleteCursor = arcpy.da.UpdateCursor(dataset, ['fclass'])

for count, d in enumerate(deleteCursor):
      if str(d[0]) in deleteValues:
           cursor.deleteRow()
del cursor

print '{0} rows were deleted.'.format(count)

Thanks.

Also will look at the Items in list as well, but I had issues with that yesterday trying to get it to work properly with the examples I found online...

0 Kudos
MitchHolley1
MVP Regular Contributor

First - I had a syntax error in my original reply.  It has been edited, please try that one. 

Second - Click the three eellipses when writing the response -> click 'More' -> 'Syntax Highlighter'

PeteJordan
Occasional Contributor III

That was what I needed, thank you.  And that's where the Syntax highlighter is, I had thought it was called something different in the past and that's why...

0 Kudos