How do I skip attributes that are null or blank?

02-26-2019 10:06 AM
Occasional Contributor III

Is there a way to check if a field is blank or null and if it's not continue the script? I can't quite get the function to work unless I'm missing something. Thanks

 if row[0] != "": # if field is not blank or null then...

import sys
import os
import arcpy

sdeconnection = "Database Connections/SDE.sde"
fc = 'Database Connections/SDE.sde/NEO.SDE.Inspections/NEO.SDE.ACInspectionSites2019'
    edit = arcpy.da.Editor(sdeconnection)  
    print "edit created"  
    print "edit started"  
    print "operation started"  
    # Perform edits
    with arcpy.da.UpdateCursor(fc, fields) as cursor:
        # For each row, evaluate the WELL_YIELD value (index position 
        # of 0), and update WELL_CLASS (index position of 1)
        for row in cursor:
            if row[0] != "": # if field is not blank or null then...
                row[1] = "Completed Inspection"
                # Update the cursor with the updated list
    print "operation stopped"  
    edit.stopEditing(True)  ## Stop the edit session with True to save the changes  
    print "edit stopped"  
except Exception as err:  
    print err  
    if 'edit' in locals():  
        if edit.isEditing:  
            print "operation stopped in except"  
            edit.stopEditing(False)  ## Stop the edit session with False to abandon the changes  
            print "edit stopped in except"  
    # Cleanup  
Tags (1)
0 Kudos
3 Replies
MVP Esteemed Contributor

Instead of giving you a specific answer, I encourage you to read /blogs/dan_patterson/2016/11/03/before-i-forget-18-those-pesky-null-things .  Building an understanding of Boolean evaluations of different data types is critical to answering this and similar questions.

MVP Legendary Contributor

I should update with additions to other 'whitespace' problems I have found since I wrote it.

But in short... generally don't try an equality check or its negation. 

If you have more than one possible 'goof' in the data preparation try 'in'

if blah not in (None, "", " "):
    then do something useful

some of the pitfalls are in the other document, but as you move toward python 3, there will be other traps you will have to be aware of

Esri Contributor

One way to do what you're looking for.

if row[0]:

or :

if len(row[0])
0 Kudos