I am trying to delete any record in my table that has a null value in a certain field (the Grid_Code field) and the code I have does not return any errors and seems to run fine, but doesn't actually delete the records with null values.
Here is my code:
with arcpy.da.UpdateCursor(fc, 'Grid_Code') as cursor:
for row in cursor:
if (row[0] == '<Null>'):
cursor.DeleteRow(row)
#I also tried adding this and got the same result:
else:
pass
Any ideas about what I am doing wrong?
2 Answers 2
Simpler Than Cursors Solution:
I'm not sure if this qualifies as an answer, since you explicitly mentioned using cursors. However, I reckon it is easier to delete features without using a cursor:
arcpy.MakeFeatureLayer_management(fc, "tmp_layer", "Grid_Code IS NULL")
arcpy.DeleteFeatures_management("tmp_layer")
To be complete, you should probably tidy up afterwards, with:
arcpy.Delete_management("tmp_layer")
Cursors Solution A:
However, if you really want to stick with cursors, you need to be testing for the NULL value, not the string "<NULL>"
. In Python, the NULL value is None
. So you could replace the if
line in your code with:
if row[0] is None:
This would give you:
with arcpy.da.UpdateCursor(fc, 'Grid_Code') as cursor:
for row in cursor:
if row[0] is None:
cursor.deleteRow()
It's also worth noting that according to the official Python Style Guide, you should use is
not ==
to test equality with the None
object (NULL value).
Cursors Solution B:
Still using cursors, but a bit simpler with one less line, using SQL instead of Python to check for the NULL value:
with arcpy.da.UpdateCursor(fc, 'Grid_Code', 'Grid_Code is NULL') as cursor:
for row in cursor:
cursor.deleteRow()
-
I was thinking about your simplest solution, what happens in the case when there are no NULL values, does the DeleteFeatures tool blitz the entire layer?Hornbydd– Hornbydd2019年01月31日 10:16:40 +00:00Commented Jan 31, 2019 at 10:16
-
1No, I don’t think so. That would be the case if you were trying to delete selected features and no features were selected. However, in this case, there would simply be no features in the layer to delete. That’s why this method is better than selecting features and then running a delete.Son of a Beach– Son of a Beach2019年01月31日 10:29:14 +00:00Commented Jan 31, 2019 at 10:29
-
should be a lower case d and empty bracket on deleteRow()Theo F– Theo F2019年10月29日 15:48:15 +00:00Commented Oct 29, 2019 at 15:48
-
Can you use these approaches to delete rows where all fields are NULL?joechoj– joechoj2023年06月21日 22:41:47 +00:00Commented Jun 21, 2023 at 22:41
Use None
.
with arcpy.da.UpdateCursor(fc, 'Grid_Code') as cursor:
for row in cursor:
if row[0] == None:
cursor.DeleteRow(row)
arcpy.da.UpdateCursor
accepts a WHERE clause -- just usewhere="GridCode is NULL"
, and you can eliminate the (incorrect)if
test. If you really want to scan the whole table, test onrow[0] == None
. You could also select rows with the WHERE clause, then use DeleteRows_managementif row[0] is None
to check for that explicitly, or justif not row[0]
to delete any rows where there's a blank-like value (like a string with no length or a number that's exactly zero).