2

I'm trying to iterate through all fields in my feature classes to find which fields are empty.

I have tried using the SearchCursor by borrowing bits of code from other answers.

This is what I have:

if fieldList:
with arcpy.da.SearchCursor(fc2, fieldList) as cursor:
    for row in cursor:
      if all(row)== NULL:
            print row.name + "is empty"

Where am I going wrong?

2

What you are doing will not work. all returns a boolean. Also, Python does not recognize NULL.

Try this:

if fieldList:
    with arcpy.da.SearchCursor(fc2, ['OID@'] + fieldList) as cursor:
        for row in cursor:
          if not filter(None, row[1:]):
                print "{} is empty".format(row[0])

Zero's will be ignored in the above case, which may be a valid value. If you want to truly check for empty values, it is better to be explicit like this:

if fieldList:
    with arcpy.da.SearchCursor(fc2, ['OID@'] + fieldList) as cursor:
        for row in cursor:
          if not filter(lambda x: x not in (None, ''), row[1:]):
                print "{} is empty".format(row[0])
  • Thanks for your comments. The code runs without error but it's not returning any results for feature classes where I know there are empty fields. Would you have any idea why that is? – JDol Aug 13 '15 at 16:29
2

My method of choice: Get your list of fields. Iterate through each field. Use list comprehension to make a list of non-null values. If your list returns empty, you know the field contains all null values. It probably isn't the fastest due to the multiple cursors, but it's hopefully easy to understand.

Code (untested):

#Input feature class
inFc = r"c:\test\test.gdb\test"

import arcpy

#get list of fields
fields = [f.name for f in arcpy.ListFields (inFc)]

#Iterate fields
for field in fields:
    #get list of values
    values = [r[0] for r
              in arcpy.da.SearchCursor (inFc, field)
              if r[0] != None]

    #Check if list is empty
    if not values:
        print field, "is empty"
  • Is this what it would look like if you were trying to find what values were NULL in one field, not the entire FC? – Purplepeopleeater Aug 25 '17 at 19:19
0

This can be determined with a single SearchCursor. Iterate each row, and when a field is determined to have a value add the field to a set of fields with values. Any field that doesn't make it in the list at the end of your cursor is a null field.

#Input feature class
inFc = r"c:\test\test.gdb\test"

import arcpy

#get list of fields
flds = [f.name for f in arcpy.ListFields (inFc)]
#get field length and indexes 
fldLen = len (flds)
fldRange = range (fldLen)

#set to store fields with values
valFlds = set ()

#create cursor
with arcpy.da.SearchCursor (inFc, flds) as curs:
    #iterate rows
    for row in curs:
        #add fields with values to set of value fields
        valFlds.update ([flds [i] for i in fldRange if row [i] != None])
        #check if all fields have been determined to have values
        if len (valFlds) == fldLen:
            #all fields have values. break cursor loop
            break

#determine null fields
nullFlds = [f for f in flds if not f in valFlds]

#check for null fields
if nullFlds:
    #iterate/print null fields
    print "null fields:"
    for fld in nullFlds:
        print fld
else:
    #no null fields
    print "no null fields"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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