1

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
2

1 Answer 1

3

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.

1
  • Don't forget to include the (required) "field_names" keyword, otherwise this works.
    – Keggering
    Commented Apr 21, 2021 at 17:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.