[GIS] How to delete all rows where a where clause is met with arcpy.da.UpdateCursor

arcpy

I would like to use a where clause that finds a certain columns, and if empty then delete the row.

My SQL where clause is;
SELECT UID2 FROM MY311CONTAINERS_2 WHERE SR_SN_2 <> ' ' AND SR_COLOR_2 <> ' ' AND SR_SIZE_2 <> ' ' AND SR_LOCATION2 <> ' '

Here is my code:

import arcpy
aContainerFC2 = 'fc'
fields = ("SR_SN_2", "SR_LOCATION_2", "Dist02", "SR_Color_2", "SR_Size_2")
whereClause = fields + " <>  ' ' "
updCurs = arcpy.UpdateCursor(aContainerFC2, whereClause)
for row in updCurs:
    if not row.getValue(field):
        updCurs.deleteRow(row)

Error:

TypeError: can only concatenate tuple (not "str") to tuple

Best Answer

I would use the power of the 'whereclause', ensuring you have all the rows you want to delete before proceeding:

qFlt = "SR_SN_2 <> ' ' AND SR_COLOR_2 <> ' ' AND SR_SIZE_2 <> ' ' AND SR_LOCATION2 <> ' '"

with arcpy.da.UpdateCursor(aContainerFC2,where_clause=qFlt) as uCur:
    for dRow in uCur:
        uCur.deleteRow ()

by the time you get to a row object you will know that it's suitable to be deleted... using the arcpy.da.UpdateCursor in a with block is prefereable because you don't need to del updCurs when you're done to release the locks.