Remove features/rows that start with

527
9
04-20-2018 09:13 AM
CCWeedcontrol
Occasional Contributor III

I have some rows that i would like to remove if they start with certain letters. I have the following code but i have to run it twice in order for it to remove certain rows, i am not sure why? I want to be able to remove rows that have the following in them "Xit",UPRR""Inpr Mp", "Impr Rxr", "I 84". How can i do this?

#delete all attributes/Rows that start with Xit, UPRR,Inpr Mp, Impr Rxr, I 84
with arcpy.da.UpdateCursor(lyr, 'STREET') as cursor:
    for row in cursor:
        if row[0].startswith("Xit"): #"UPRR","Inpr Mp", "Impr, "Rxr", "I 84"
            cursor.deleteRow()
0 Kudos
9 Replies
DanPatterson_Retired
MVP Emeritus

to demonstrate, you need a bit of a check...

row0 = 'Inpr Mp'

vals = ["UPRR","Inpr Mp", "Impr", "Rxr", "I 84"]

for i in vals:
    if row0.startswith(i):
        print("delete this")
        vals.remove(i)
    else:
        print("OK {}".format(vals))
        
OK ['UPRR', 'Inpr Mp', 'Impr', 'Rxr', 'I 84']
delete this
OK ['UPRR', 'Impr', 'Rxr', 'I 84']
OK ['UPRR', 'Impr', 'Rxr', 'I 84']
RandyBurton
MVP Alum

Building on Dan's idea, perhaps:

vals = ["Xit","UPRR","Inpr Mp","Impr", "Rxr", "I 84"]
row0 = "I 84 Exit 21"
for v in vals:
    if row0.startswith(v):
        print "Delete row with {}".format(row0)

# prints:  Delete row with I 84 Exit 21

#delete all attributes/Rows that start with Xit, UPRR,Inpr Mp, Impr Rxr, I 84
vals = ["Xit","UPRR","Inpr Mp","Impr", "Rxr", "I 84"]
with arcpy.da.UpdateCursor(lyr, 'STREET') as cursor:
    for row in cursor:
        for v in vals:
            if row[0].startswith(v):
                cursor.deleteRow()‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
DarrenWiens2
MVP Honored Contributor

Even though it's ugly, I suspect you'll see better performance using a where clause in the cursor and delete all rows. Untested, and you might have to play with the quotes:

with arcpy.da.UpdateCursor(lyr, 'STREET', whereclause=""" STREET LIKE 'Xit%' OR STREET LIKE 'UPRR%' OR STREET LIKE 'Inpr Mp%' OR STREET LIKE 'Impr%' OR STREET LIKE 'Rxr%' OR STREET LIKE 'I 84%' """) as cursor:‍‍‍
CCWeedcontrol
Occasional Contributor III

I couldn't get it to work like you posted. I had to move the whereclause out of the UpdateCursor.

where = "STREET LIKE 'Xit%' OR STREET LIKE 'Uprr%' OR STREET LIKE 'Inpr%' OR STREET LIKE 'Impr%' OR STREET LIKE 'Rxr%' OR STREET LIKE 'I 84%'OR STREET LIKE 'Hwy%'"

with arcpy.da.UpdateCursor(lyr, 'STREET', where) as cursor:
    for row in cursor:
        cursor.deleteRow()
0 Kudos
CCWeedcontrol
Occasional Contributor III

why do you guys have row0 = "I 84 Exit 21" and 'Inpr Mp'shouldn't it row0 = "STREET"?

0 Kudos
RandyBurton
MVP Alum

We were using row0 so that for illustration purposes the variable name looks similar to row[0] , the first element in the SearchCursor row.  This would be equal to the contents of the "STREET" field.

JoshuaBixby
MVP Esteemed Contributor

A couple of examples posted here can be simplified a bit because str.startswith() accepts a tuple, thus removing the need to explicitly loop over your list of prefixes.

prefixes = ("Xit", "UPRR", "Inpr Mp", "Impr Rxr", "I 84")
with arcpy.da.UpdateCursor(lyr, 'STREET') as cursor:
    for street, in cursor:
        if street and street.startswith(prefixes):
            cursor.deleteRow()

For line #4, I added the extra if street to prevent an error if the street field is null.

CCWeedcontrol
Occasional Contributor III

Not sure but it's not working, i don't get any errors. It doesn't remove any of these.

0 Kudos
CCWeedcontrol
Occasional Contributor III

I figured out why some of the attributes did not have the first letter capitalized.

0 Kudos