利用arcpy实现批量读取矢量文件属性表:(1)读取每个矢量文件包含多少个对象 (2)读取每个矢量文件每个字段对应的值。
#导入包
>>> import os
>>> import arcpy
#设置默认的工作环境
>>> arcpy.env.workspace=r"******"
#读取默认工作环境中得所有要素文件(.shp)文件,并将文件名保存为列表List
>>> List=arcpy.ListFeatureClasses()
#读取文件夹中得第一个shp文件得所有字段名称
>>>for i in List[0:1]:
... fields=arcpy.ListFields(i)
... for f in fields:
... print(f.name)
输出结果:
FID
Shape
GRIDCODE
Area
#说明shp文件属性表中有FID、Shape 、GRIDCODE和Area四个字段
#依次读取shp文件中Area字段的值
for i in List:
... shpfields=["Area"]
... shp_List = []
... shprows = arcpy.SearchCursor(i, shpfields)
... while True:
... shprow = shprows.next()
... if not shprow:
... break
... shp_List.append(shprow.Area)
... for j in range(0,len(shp_List)):
... print(i,shp_List[j])
输出结果:
(u'20181002t025539_20181002t025937_t50tmk.shp', 142027662.006)
(u'20181005t030549_20181005t030637_t50tmk.shp', 143648362.606)
(u'20181007t025601_20181007t030202_t50tmk.shp', 142296982.028)
(u'20181017t025711_20181017t030259_t50tmk.shp', 143914358.642)
(u'20181027t025811_20181027t030402_t50tmk.shp', 146175457.06)
......
(u'20220419t025551_20220419t030348_t50tmk.shp', 161952378.851)
(u'20220502t030551_20220502t031405_t50tmk.shp', 159937453.891)
(u'20220522t030551_20220522t031519_t50tmk.shp', 157370909.99)
(u'20220601t030551_20220601t031520_t50tmk.shp', 157310955.682)
(u'20220623t025529_20220623t030357_t50tmk.shp', 157979052.551)
参考资料
https://blog.csdn.net/gognzixiaobai666/article/details/112666377