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
3 Answers 3
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 return0
because there are noNone
values in the list. Instead there will be a tuple(None, None)
for all-None
rows.Emil Brundage– Emil Brundage2019年08月22日 18:35:32 +00:00Commented Aug 22, 2019 at 18:35 -
With append that would be true, not with extendBera– Bera2019年08月22日 18:36:34 +00:00Commented Aug 22, 2019 at 18:36
-
1ah my mistake. I always use append.Emil Brundage– Emil Brundage2019年08月22日 18:36:59 +00:00Commented Aug 22, 2019 at 18:36
@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))
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