python处理要素类与excel关联的数据
当需要将要素类与excel关联时,不使用控制面板而通过arcpy的方式进行操作,极大的简化操作,提高效率。
先操作excel读出对应的列的数据,存放到一个list内,然后遍历要素类QSZ字段数据,判断是否在list内即可。
下面为代码片段:
>>> x = "E:/wz.xls"
>>> data = xlrd.open_workbook(x)
>>> table = data.sheet_by_index(0)
>>> nrows = table.nrows
>>>list = []
>>> for row in range(1,nrows):
... name = table.cell(row,0).value
... list.append(name)
>>> l = "E:/textdata/wz.gdb/zd"
>>> with arcpy.da.SearchCursor(l,("QSZ")) as cursor:
... for r in cursor:
... if (r[0] not in list):
... print r[0]
结果即为未能匹配的QSZ数据。
# -*-coding:utf-8 -*-
import arcpy
import xlrd
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
excelFile = arcpy.GetParameterAsText(0)
data = xlrd.open_workbook(excelFile)
list1 = []
list2 = []
table1 = data.sheet_by_index(3)
nrows1 = table1.nrows
table2 = data.sheet_by_index(4)
nrows2 = table2.nrows
table3 = data.sheet_by_index(5)
nrows3 = table3.nrows
zdFile = arcpy.GetParameterAsText(1)
finalFile = arcpy.GetParameterAsText(2)
################库有表无######################
textFile1 = open(finalFile+"\KuYouBiaoWu.txt","w")
for row1 in range(2,nrows1):
name1 = table1.cell(row1,15).value
list1.append(name1)
for row2 in range(1,nrows2):
name2 = table2.cell(row2,15).value
list1.append(name2)
for row3 in range(1,nrows3):
name3 = table3.cell(row3,15).value
list1.append(name3)
with arcpy.da.SearchCursor(zdFile,("QSZ")) as cursor:
for r in cursor:
if (r[0] not in list1):
textFile1.write(r[0]+'\n')
textFile1.close()
################表有库无######################
textFile2 = open(finalFile+"\BiaoYouKuWu.txt","w")
with arcpy.da.SearchCursor(zdFile,("QSZ")) as cursor:
for r in cursor:
list2.append(r[0])
for row1 in range(2,nrows1):
name1 = table1.cell(row1,15).value
if(name1 not in list2):
textFile2.write(name1+'\n')
for row2 in range(2,nrows2):
name2 = table2.cell(row2,15).value
if(name2 not in list2):
textFile2.write(name2+'\n')
for row3 in range(2,nrows3):
name3 = table3.cell(row3,15).value
if(name3 not in list2):
textFile2.write(name3+'\n')
textFile2.close()
转载自:https://blog.csdn.net/qq_38350792/article/details/77371926