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

问题描述 投票:0回答:1

我有一个主文件夹,它有多个子文件夹。每个子文件夹都有一个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)
python arcgis arcpy arcmap
1个回答
0
投票

我认为,在使用游标读取每一行之前,首先使用sql查询来检查是否有None值应该更快(btw使用数据访问游标(da.SearchCursor),它们要快得多)。虽然不确定处理shapefile时的速度。试试:

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))
© www.soinside.com 2019 - 2024. All rights reserved.