Select to view content in your preferred language

Delete Rows where value is Null

11584
4
Jump to solution
07-19-2012 11:54 AM
KarstenRank
Frequent Contributor
Hello,

I have a table in a geodatabase where the value of a field is sometimes <Null>. How can I delete these rows?

Thanks for your help!

Best,
Karsten
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
MarcinGasior
Frequent Contributor
You have to provide valid SQL where clause and defined variable in getValue() function.
Here's an example:
import arcpy EingabeTabelle = r"C:\tmp\Test.gdb\YourTable" field = "Liste" whereClause = field + " IS NULL"  updCurs = arcpy.UpdateCursor(EingabeTabelle, whereClause) for row in updCurs:     if not row.getValue(field):         updCurs.deleteRow(row)

View solution in original post

0 Kudos
4 Replies
MathewCoyle
Honored Contributor
Off the top of my head I can think of two ways.

Option 1: Cursor
up_curs = arcpy.UpdateCursor(fc,"FIELD IS NULL")
for row in up_curs:
    if not row.getValue(FIELD):
        row.deleteRow(row)


Option 2: Select/Delete Features
arcpy.MakeFeatureLayer_management(fc,layer)
arcpy.SelectLayerByAttribute_management(layer,"FIELD IS NULL")
desc = arcpy.Describe(layer)

feat_count = int(arcpy.GetCount_management(parcel).getOutput(0))
feat_ids = desc.FIDSet

if feat_count > 0 and len(feat_ids) > 0:
    arcpy.DeleteFeatures_management(layer)


Note: I haven't tested either of these so make sure you do some trials in a test environment.
0 Kudos
KarstenRank
Frequent Contributor
If I insert the field name "Liste", I get the error message <type 'exceptions.NameError'>: name 'Liste' is not defined.[ATTACH=CONFIG]16269[/ATTACH]

up_curs = arcpy.UpdateCursor(EingabeTabelle,"Liste IS NULL")
for row in up_curs:
    if not row.getValue(Liste):
        row.deleteRow(row)
0 Kudos
MarcinGasior
Frequent Contributor
You have to provide valid SQL where clause and defined variable in getValue() function.
Here's an example:
import arcpy EingabeTabelle = r"C:\tmp\Test.gdb\YourTable" field = "Liste" whereClause = field + " IS NULL"  updCurs = arcpy.UpdateCursor(EingabeTabelle, whereClause) for row in updCurs:     if not row.getValue(field):         updCurs.deleteRow(row)
0 Kudos
KarstenRank
Frequent Contributor
Thank you! This  solved my probl

Best,
Karsten
0 Kudos