如何在*多个*形状文件的字段中检查空值



我有一个主文件夹,它有多个子文件夹。每个子文件夹都有一个形状文件。我想测试形状文件中所有具有空值的字段。如果存档的形状文件具有空值,则打印形状文件名和字段名。

我找到了代码,但它只适用于一个形状文件。

import arcpy
fc = r'C:Y4YKMuni'
fields = dict((f.name, []) for f in arcpy.ListFields(fc) if not f.required)
rows = arcpy.SearchCursor(fc,"","","","")
for row in rows:
for f in fields.keys():
fields[f].append(row.getValue(f))
for field, values in fields.iteritems():
if any(map(lambda s: s is None or not str(s).strip(), values)):
print 'Field: "{}" has empty values'.format(field)

我认为在使用游标读取每一行之前,首先使用sql查询检查是否有任何行中有None值应该更快(顺便说一下,使用数据访问游标(da.SearchCursor(,它们要快得多(。

尝试:

import arcpy, os
arcpy.env.overwriteOutput = True #To be able to use same layer name in MakeFeatureLayer
shapefolder = r'C:GISdatatestdata'
for path, subdirs, files in os.walk(shapefolder):
for name in files:
if name.endswith('.shp'):
shapefile = os.path.join(path,name)
fields_to_check = [f.name for f in arcpy.ListFields(shapefile) if not f.required]
sql = ' OR '.join([field+" IS NULL" for field in fields_to_check]) #Construct sql query like: 'Field1 IS NULL OR Field2 IS NULL OR ...'
arcpy.MakeFeatureLayer_management(in_features=shapefile, out_layer='layer', where_clause=sql) #Use the sql clause to create a temporary layer
shapefile_row_count = int(arcpy.GetCount_management(in_rows=shapefile).getOutput(0))
if int(arcpy.GetCount_management(in_rows='layer').getOutput(0)) >= shapefile_row_count and shapefile_row_count >0: #Check if row number returned by query are >= to shapefile row count
nonefields = []
with arcpy.da.SearchCursor('layer', fields_to_check) as cursor:
for row in cursor:
if None in row:
nones = [fields_to_check[j] for j in [i for i in range(len(row)) if row[i] is None]]
nonefields.extend(nones)
nonefields = ', '.join(sorted(list(set(nonefields))))
print 'None value(s) in shapefile: {}, field(s): {}'.format(shapefile, nonefields)

应该输出以下内容:

None value(s) in shapefile: C:GISdatatestdataintertest.shp, field(s): fieldname1, fieldname2, fieldname10
None value(s) in shapefile: C:GISdatatestdatapolygons.shp, field(s): blabla, blablabla

最新更新