[GIS] How to check for empty values in fields of a featureclass

arcgis-10.0arcpy

(ArcGIS Desktop 10.0 Python 2.7)

I need to check if any empty fields exist in featureclass fields in a gdb. These could be NONE, NULL "". The field name here is "Stand".

The following code doesn´t seem to work as "Test" is printed for every feature class even when they are filled!

rows = arcpy.SearchCursor(fc,"","","","")
        for row in rows:
            try:
       #check to see if field "Stand" has empty rows and print message to confirm
                if row.Stand:
                    print "empty row!"
                    break
            except:
                print "Feld existiert nicht"

Best Answer

I just wanted to point out that just using if not row.Stand may yield undesired results (remember, explicit is better than implicit from the Zen of Python). Take the following example:

>>> for i, sample in enumerate(['a',1,None, '', ' ', 0]):
    if not sample:
        print 'value "{}" at index {} is empty'.format(sample, i)


value "None" at index 2 is empty
value "" at index 3 is empty
value "0" at index 5 is empty

We can see here that a single space returns true, because it is a character. We are also removing zeros (which may or may not be a "NULL" value, depends on the situation).

Instead, it may be better to test if it is None or if the string contains any non-whitespace characters:

>>> for i, sample in enumerate(['a',1,None, '', ' ', 0]):
    if sample is None or not str(sample).strip():
        print 'value "{}" at index {} is empty'.format(sample, i)


value "None" at index 2 is empty
value "" at index 3 is empty
value " " at index 4 is empty

Now we have removed any values that are just white space. This may not be a big issue, but me having prior work experience in an assessors office, there were plenty of parcel fields that were supposed to have values and only contained white space would not be flagged in the simple if not row.Field example. Here is what you can use to test for a single field:

rows = arcpy.SearchCursor(fc,"","","","")
for row in rows:
   if row.Stand is None or not str(row.Stand).strip():
        print 'empty row!'
        break

Or, if you want to test all fields:

import arcpy
fc = r'C:\TEMP\Huxley.gdb\Watermain\WtrValve'
fields = dict((f.name, []) for f in arcpy.ListFields(fc) if not f.required)

rows = arcpy.SearchCursor(fc,"","","","")
for row in rows:
    for f in fields.keys():
        fields[f].append(row.getValue(f))

for field, values in fields.iteritems():
    if any(map(lambda s: s is None or not str(s).strip(), values)):
        print 'Field: "{}" has empty values'.format(field)

This yielded:

>>> 
Field: "VAL_TYPE" has empty values
Field: "GNSS_HEIGHT" has empty values
Field: "HL_ACCU" has empty values
Field: "PHOTO_1" has empty values
Field: "COMMENTS" has empty values
Field: "PROJ_NUMB" has empty values
Field: "PDF_LOC" has empty values
Field: "YR_INST" has empty values
Field: "TOP_COND" has empty values
Field: "VAL_USE" has empty values
Field: "ASBUILT" has empty values
Field: "PROJ_NAME" has empty values
Related Question