import arcpy in_workspace = r"C:/data " fields = ["A", "B", "C", "D", "E", "F", "G"] where = fields = "None" for dirpath, dirnames, filenames in arcpy.da.Walk(in_workspace, datatype="featureclass", type="Polyline"): for filename in filenames: with arcpy.da.SearchCursor(filename, fields, where) as cursor: for row in cursor: print filename, "has null values"
I am trying to use the search cursor to find all null values within a table but am having a hard time with the where-clause statement. How do i search for null values using searchcursor?
fields = ["A", "B", "C", "D", "E", "F", "G"] where = ['"{0}" IS NULL'.format(f) for f in fields] where = " OR ".join(where) # where = "A" IS NULL OR "B" IS NULL OR ... for dirpath, dirnames, filenames in arcpy.da.Walk(in_workspace, datatype="featureclass", type="Polyline"): for filename in filenames: path = os.path.join(dirpath, filename) # see example 2 in the help for arcpy.da.walk lyr = arcpy.MakeFeatureLayer_management(path, "lyr", where) rows = int(arcpy.GetCount_management(lyr).getOutput(0)) if rows > 0: print filename, " has null values"
fields = [f.name for f in arcpy.Describe(path).Fields] where = " OR ".join(['"{0}" IS NULL'.format(f) for f in fields]) lyr = arcpy.MakeFeatureLayer_management(path, "lyr", where)
import arcpy from arcpy import env env.workspace = r"C:\temp\python\test.gdb" fields = ["OBJECTID", "Name", "Address"] for dirpath, dirnames, filenames in arcpy.da.Walk(env.workspace): for filename in filenames: for field in fields: where = field + " IS NULL" try: with arcpy.da.SearchCursor(filename, fields, where) as cursor: for row in cursor: print("OBJECTID {0}").format(row[0]) + " in " + filename + " has a NULL value in field " + field except RuntimeError: pass del row, cursor
Your where expression is poorly formed, and you missed a step in the arcpy.da.walk.
I would suggest avoiding the cursor altogether:fields = ["A", "B", "C", "D", "E", "F", "G"] where = ['"{0}" IS NULL'.format(f) for f in fields] where = " OR ".join(where) # where = "A" IS NULL OR "B" IS NULL OR ... for dirpath, dirnames, filenames in arcpy.da.Walk(in_workspace, datatype="featureclass", type="Polyline"): for filename in filenames: path = os.path.join(dirpath, filename) # see example 2 in the help for arcpy.da.walk lyr = arcpy.MakeFeatureLayer_management(path, "lyr", where) rows = int(arcpy.GetCount_management(lyr).getOutput(0)) if rows > 0: print filename, " has null values"
If you want to look at all fields no matter what they are named, you can try this inside the loop:fields = [f.name for f in arcpy.Describe(path).Fields] where = " OR ".join(['"{0}" IS NULL'.format(f) for f in fields]) lyr = arcpy.MakeFeatureLayer_management(path, "lyr", where)
Hi Chris,
When querying NULL values, you will want to use the syntax "Field IS NULL". You will also want to iterate through your fields list and set the query up for each field name. Here is an example:import arcpy from arcpy import env env.workspace = r"C:\temp\python\test.gdb" fields = ["OBJECTID", "Name", "Address"] for dirpath, dirnames, filenames in arcpy.da.Walk(env.workspace): for filename in filenames: for field in fields: where = field + " IS NULL" try: with arcpy.da.SearchCursor(filename, fields, where) as cursor: for row in cursor: print("OBJECTID {0}").format(row[0]) + " in " + filename + " has a NULL value in field " + field except RuntimeError: pass del row, cursor
# Import the arcpy module import arcpy # Location of the feature class I want to update gdb = "C:\\Data\\NothingSpecial.gdb\\" fcs = ["calc","table1","etc"] for item in fcs: fc = gdb + item # Create a cursor using arcpy data access module sc = arcpy.da.SearchCursor(fc, "*") print "Null values in feature class: "+fc.rsplit("\\",1)[1] print "===================================================" for row in sc: if None in row: print ("Null value in row {0}".format(row[0]))
Thank you for the suggestion and helpful script. When I run this on a larger folder (100+) it crashes IDLE. But ran nicely on only 5 or so folders. Thoughts?