5

(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"
2
  • Hi Robert. I assume Stand is the field name. The code looks fine to me... Can you provide the error message? My only comment was you probably only need if row.Stand: or if row.getValue("Stand"): without the == equality in line 4, since "" and None will both evaluate to False in Python anyway.
    – John
    Commented Jul 28, 2015 at 13:20
  • Oh my apologies, you just need a "not" to negate the condition. So if not row.Stand: or if not row.getValue("Stand"):. Does that solve the problem?
    – John
    Commented Jul 28, 2015 at 13:29

2 Answers 2

5

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
1
  • Note, the second fields sample may be kind of slow using the 10.0 cursors, could handle it much faster if you had access to the arcpy.da cursors introduced at version 10.1
    – crmackey
    Commented Jul 28, 2015 at 15:05
2

Following @crmackey with his mention of using the da (Data Access) module, the following code is pretty quick for this type of thing. However, as mentioned it requires 10.1 or higher. I have found that the da module is far superior when it comes to running through tables and performing tests on the data in ArcGIS.

with arcpy.da.SearchCursor(fc, ("OBJECTID", "Stand")) as cursor:
    for row in cursor:
        if (row[1] is None):
            print "OBJID: " + str(row[0]) + " ----> Nothing here!"
        else:
            print "OBJID: " + str(row[0]) + " ----> " + str(row[1])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.