AnsweredAssumed Answered

da.UpdateCursor not deleting Null rows

Question asked by IMakeNiceMaps on Mar 12, 2020
Latest reply on Mar 13, 2020 by IMakeNiceMaps

I have a fairly simple problem, and I've tried everything I can think of to fix it. I am using a data access update cursor in a larger script to update the value of one field, and if the other field is Null I want to delete the record. The update part works fine, but the delete part does not. 

 

My input data is an annotation feature class created by ArcMap (turn on labels, right-click layer, create annotation). Nothing fancy or complex. The annotation expression is stored in a field named TextString. If no annotation is created for the feature, the field is Null (as a default, all annotation classes created by ArcMap are nullable). When you look at it the attribute table it appears as <Null>. 

 

I cannot use a where clause in this case because of the first update.

 

The following example should work as far as I can tell, but doesn't. 

 

with arcpy.da.UpdateCursor (in_table="RoadsAnno", field_names=["Status","TextString"]) as cursor:
    for row in cursor:
        row[0] = 0
        cursor.updateRow(row)
        if row[1] is None:
            cursor.deleteRow()
del cursor, row

(I also tried replacing "if row[1] is None:" with "if row[1] == None:" but no joy.  "is None:" is the most proper way to write this according to my Python resources, though "==  None:" still works (in other situations)

 

So I then tried the following:

 

with arcpy.da.UpdateCursor (in_table="RoadsAnno", field_names=["Status","TextString"]) as cursor:
    for row in cursor:
        row[0] = 0
        cursor.updateRow(row)   
        if row[1] is None or row[1] =="" or row[1] == "<Null>":
            cursor.deleteRow()
del cursor, row

 

I figured one of those statements would catch the Null value, but it did not.

 

I then changed the value of a previously Null row in the RoadsAnno feature class to read "DeleteMe" (not in quotes) and ran the following just to test if the cursor was picking up the right field:

 

with arcpy.da.UpdateCursor (in_table="RoadsAnno", field_names=["Status","TextString"]) as cursor:
    for row in cursor:
        row[0] = 0
        cursor.updateRow(row)   
        if row[1] is None or row[1] =="DeleteMe" or row[1] == "<Null>":
             cursor.deleteRow()
del cursor, row

 

The row containing the TextString attribute "DeleteMe" was deleted. So what's happening with the Null value?

 

Would anyone be able to help with this? Why doesn't the cursor recognize the Null value in the field?  Maybe Arc creates Null values with unseen characters or something that need to be stripped?  

 

EDIT:  Do I have to put the updateRow statement after the deleteRow statement?  Maybe the cursor is moving to the next row with the update and never gets the chance to delete?  Although that wasn't the case for the "DeleteMe" example so perhaps this line of thought is not correct.

Outcomes