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

How to check for empty values in fields of *multiple* shape files?

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

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

import arcpy
fc = r'C:\Y4YK\Muni'
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:\GIS\data\testdata'

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:\GIS\data\testdata\intertest.shp, field(s): fieldname1, fieldname2, fieldname10
None value(s) in shapefile: C:\GIS\data\testdata\polygons.shp, field(s): blabla, blablabla