SearchCursor Select Attributes Delete Rows

304
4
03-20-2024 02:59 PM
ModernElectric
Occasional Contributor III

I am trying to figure out how to write a Python script that will use Search Cursor to go thru a Feature Class based on a list of attributes in the script and delete the entire row.

 I am not the best code writer, so please bare with me. This is what I have but nothing is working:

expression = ('45074.0115', '45074.0116', '45074.0117', '45074.0118', '45074.0119', '45074.0120')

with arcpy.da.UpdateCursor(MEWCo_PARCELS_UPDATE, "PID", expression) as cursor:
    for row in cursor:
        row[0] = 0
        cursor.deleteRow()‍‍‍)

I know 100% the code is written incorrectly, just not sure what part.

MEWCo_PARCELS_UPDATE is the FC

PID is the field

Thank You

0 Kudos
4 Replies
JoshuaBixby
MVP Esteemed Contributor

Not to get hung up on semantics, but the title and description say "Search Cursor" but the code says "UpdateCursor".  I assume the code is correct?

Tom_Laue
New Contributor III

Yes, that is correct. You need to use update cursor to edit records whereas a search cursor is only used for read-only. 

BlakeTerhune
MVP Regular Contributor

Your expession is not valid. You need to make it a string variable, include a field name, and use some kind of statement or operator with that set of values. Something like:

expression = "MyField in('45074.0115', '45074.0116', '45074.0117', '45074.0118', '45074.0119', '45074.0120')"

Also, you have numbers formatted as text. Is that correct? And I don't understand the purpose of the row[0] = 0

Clubdebambos
Occasional Contributor III

This will get you there. Remove the row[0] = 0 and just make sure you test on a copy of your data before you perform the delete! You are new to coding/arcpy so you need to make sure the code is working exactly as expected. Depending on your working environment you might not be able to retrieve deleted features. 

~ learn.finaldraftmapping.com
0 Kudos