2

I would like to check two fields in each tables which name start with X_ into a database !

I've started to write something like :

arcpy.env.workspace = "C:/myWorkspace.sde"
tables = arcpy.ListTables("X_*")
for table in tables:
    print(table)

But now I'm stuck for searching into the tables if there is Field_A and Field_B only filled with NULL values

1

Use the da.SearchCursor to create a list of all entries then check list:

import arcpy

arcpy.env.workspace = "C:/myWorkspace.sde"
tables = arcpy.ListTables("X_*")
for table in tables:
    all_rows = []
    with arcpy.da.SearchCursor(table, ['FieldA','FieldB']) as cursor:
        for row in cursor:
            all_rows.extend(row)
    if all_rows.count(None) == len(all_rows):
        print('{0} is all none'.format(table))
  • all_rows.count(None) will always return 0 because there are no None values in the list. Instead there will be a tuple (None, None) for all-None rows. – Emil Brundage Aug 22 at 18:35
  • With append that would be true, not with extend – BERA Aug 22 at 18:36
  • 1
    ah my mistake. I always use append. – Emil Brundage Aug 22 at 18:36
1

@BERA has a good answer but it can be made more efficient by skipping the creation of lists and by breaking the iteration of the cursor if a value is found.

import arcpy

arcpy.env.workspace = "C:/myWorkspace.sde"
tables = arcpy.ListTables("X_*")
for table in tables:
    vals = False
    with arcpy.da.SearchCursor(table, ['FieldA','FieldB']) as cursor:
        for row in cursor:
            if row [0] != None or row [1] != None:
                print ("{} fields are not null".format (table))
                vals = True
                break
    if not vals:
        print ("{} fields are null".format (table))
0

Thanks a lot for your help both of you ! I finally done something like that :

import arcpy
arcpy.env.workspace = "C:/myWorkspace.sde"
tables = arcpy.ListTables("X_*")
emptyTables = []
print "--- Tables where fields Field_A or Field_B are empty or null :"
for table in tables:
    vals = False
    with arcpy.da.SearchCursor(table, ['Field_A', 'Field_B']) as cursor:
        for row in cursor:
            if row[0] == 'NULL' or row[0] == None or row[1] == 'NULL' or row[1] == None :
                 print table
                 vals = True
                 break
     if not vals:
        emptyTables.append(table)
print "------------------"
print "--- Empty Tables :"
for emptyTable in emptyTables:
    print emptyTable

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.