python处理要素类与excel关联的数据
2017-08-18 16:39
183 查看
当需要将要素类与excel关联时,不使用控制面板而通过arcpy的方式进行操作,极大的简化操作,提高效率。
先操作excel读出对应的列的数据,存放到一个list内,然后遍历要素类QSZ字段数据,判断是否在list内即可。
下面为代码片段:
结果即为未能匹配的QSZ数据。
先操作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()
相关文章推荐
- 关于excel数据来源单据和母单据之间的关联
- java poi Excel内图片获取存储并关联数据入库
- 使用游标将excel不同表的列关联,并根据两表间相同的列值逐行更新数据
- 单据excel数据付款关联取数实现
- excel多列合并关联数据
- excel 关联数据
- 使用js将多个相关联的excel数据文件整合到一起并筛选出需要的数据的整理
- Excel跨表查询关联数据
- java easyreport 导入excel、 txt 数据关联集合置入对象(三)
- 怎样关联多个 Excel 档工作表(Sheet)中的数据
- 在excel中把一些数据相关联起来的方法
- Excel将两个表格关联数据
- excel数据导入oracle数据库
- Excel中工作表间的关联链接方法
- 将excel里面的数据导入到程序里面
- World Wide Web Project EXCEL导入数据
- MATLAB怎样有效读取excel文件中的数据?
- 从数据库导出数据生成word或excel文档
- 无法读取Excel中的数据单元格。有数据,但是读出来全是空值
- C#读取Excel表中的数据时,为何有些行的字段内容读取不到