Function to delete records

7378
11
05-19-2016 10:45 AM
LianaSmith
Occasional Contributor II

I need a small code in python that will delete records that have empty value in a specific field of the layer. The name of the feature class is WCSCustomer and the name of the field is GROUTE

0 Kudos
11 Replies
AdrianWelsh
MVP Honored Contributor

Hi Liana,

Take a look at this thread:

Delete Rows where value is Null

I believe this will have the solution that you need.

LianaSmith
Occasional Contributor II

Thanks, Adrian. I have just empty value, not Null.. I am not sure if I can change the script for that myself..

0 Kudos
AdrianWelsh
MVP Honored Contributor

maybe try

import arcpy

fc = r'C:\tmp\Test.gdb\WCSCustomer'
field = "GROUTE"
whereClause = field + ' = ""'

updCurs = arcpy.UpdateCursor(fc, whereClause)
for row in updCurs:
    if not row.getValue(field):
        updCurs.deleteRow(row)
0 Kudos
LianaSmith
Occasional Contributor II

Adrian,

I tried to ran it and I get an exception..

0 Kudos
AdrianWelsh
MVP Honored Contributor

yeah, an issue with the SQL statement.

Since it's in a file geodatabase, could you just use the Null value approach?

import arcpy


fc = r"C:\tmp\Test.gdb\WCSCustomer"
field = "GROUTE"
whereClause = field + " IS NULL"


updCurs = arcpy.UpdateCursor(fc, whereClause)
for row in updCurs:
    if not row.getValue(field):
        updCurs.deleteRow(row)

Or use Mitch's example of if x is none, etc....

0 Kudos
curtvprice
MVP Esteemed Contributor

In this whole thread I don't see the full example.

My code will zap the row if the field contents is spaces, empty string, or Null.

fc = r"C:\tmp\Test.gdb\WCSCustomer"
field = "GROUTE"
with arcpy.da.UpdateCursor(fc, field) as rows:
    for row in rows:
        try:
            val = row[0].strip()
        except:
            val = row[0]
        if val in ["", None]:
            rows.deleteRow(row)
MitchHolley1
MVP Regular Contributor

Use None instead of "Null"

for x in this:

     if x is None:

          delete that stuff

DanPatterson_Retired
MVP Emeritus

or alternately

do the row.getvalue or row[index number] thing ( we will call it x using Mitch's designation)

then....

    if x in [None,""]:

        delete stuff

The rationale is that an empty records is going to show <null> in the table, but python returns None and shapefiles store "" and other stuff stores one of those two (if memory serves).  The nice thing is you can add anything you want to the list.  the python 'isinstance' can also be used, but then you have to remember the class and None is a NoneType or which there is only one instance of that class.

JoshuaBixby
MVP Esteemed Contributor

I assume you are working with text fields since you are talking about empty being different than Null (or None in Python).  When working with text fields, just be aware there are some characters and numerous control characters that don't display (the field will look empty in ArcMap/ArcCatalog) but prevent !field! == "" from being True.

It might be worth looking over the following discussion:  I am trying to calculate a field where i want to exclude any null values. For example. Field calulat...