|
通过读取Excel数据,获得需要修改的属性值,之后通过对相应的值进行判断,进而对数据的属性进行修改
进行Python编程时候,设置编码方式尤为重要,
# -*- coding:UTF-8 -*-
本篇博客内容包含了
(1)Excel的读取
(2)Python对字典的创建、赋值、取值等操作
(3)对gdb数据的读取
(4)对要素的查找和属性值的修改
具体的代码示例如下:
# -*- coding:UTF-8 -*- import arcpy import xlrd # 获取POI以及对应的编码 def getPOIIDs(excelPath): dataExcel = xlrd.open_workbook(excelPath) table01 = dataExcel.sheet_by_index(0) dicPOINames = {} dicPOICodes = {} for i in range(1, table01.nrows): dicPOINames[table01.cell_value(i, 0)] = table01.cell_value(i, 3) # 获取Excel的值并存入dic字典中 dicPOICodes[table01.cell_value(i, 7)] = table01.cell_value(i, 8) print return dicPOINames, dicPOICodes def CodeFeature(fcPath, dicPOINames, dicPOICodes, atName, atID): cursor = arcpy.UpdateCursor(fcPath) iindex = 0 errorindex = 0 for row in cursor: value = row.getValue(atName) if value != "": atname = dicPOINames[value] if atname != "": try: row.setValue(atID, dicPOICodes[atname]) cursor.updateRow(row) iindex += 1 except Exception as e: errorindex += 1 print("第" + str(iindex) + "条要素编码赋值失败。") continue print "总共修改" + str(iindex) + "条数据,其中错误的有" + str(errorindex) + "条" gdbPath = r"C:\Users\Administrator\DesktopPOI.gdb\POI" excelPath = r"C:\Users\Administrator\Desktop\WorkData\targetID.xls" dicPOINames = {} dicPOICodes = {} atName = "LX" atID = "CLASID" try: (dicPOINames, dicPOICodes) = getPOIIDs(excelPath) except Exception as e: print "读取Excel数据有问题。" print e.message CodeFeature(gdbPath, dicPOINames, dicPOICodes, atName, atID)
参考:https://blog.csdn.net/imxiezy/article/details/88698846
Archiver|手机版|科学网 ( 京ICP备07017567号-12 )
GMT+8, 2024-9-20 01:43
Powered by ScienceNet.cn
Copyright © 2007- 中国科学报社