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

TomazicM
27.3k25 gold badges33 silver badges43 bronze badges
asked Aug 22, 2019 at 15:48
0

3 Answers 3

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))
answered Aug 22, 2019 at 17:28
3
  • 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. Commented Aug 22, 2019 at 18:35
  • With append that would be true, not with extend Commented Aug 22, 2019 at 18:36
  • 1
    ah my mistake. I always use append. Commented Aug 22, 2019 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))
answered Aug 22, 2019 at 18:30
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
answered Aug 23, 2019 at 10:01

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.