配置文件格式如下:
引入了读取Excel格式xlsx格式的第三方库xlrd(xlrd-0.9.3.tar.gz)
具体Python代码如下:
# -*- coding: utf-8 -*- # --------------------------------------------------------------------------- # Merge.py # Created on: 2013-01-21 10:25:22.00000 # (generated by WangLin_TJCH) # Description: # --------------------------------------------------------------------------- # Import arcpy module import arcpy import os.path import time import sys import random import xlrd from arcpy import env excelconfig = "中英文图层名称对照.xlsx" FCDBDir = "D:\\第一批国情普查检查\\复件 要素数据" # key存放中文 value存放英文 dicEnCnFC={} fcall=[] GDBAllPath=[] # 获取脚本运行目录 os.getcwd() # 获取脚本所在目录 os.path.split(os.path.realpath(sys.argv[0]))[0] fullconfig = os.path.join(os.getcwd(),excelconfig) if os.path.isfile(fullconfig): workbook = xlrd.open_workbook(fullconfig) try: sh = workbook.sheet_by_index(0) nrows = sh.nrows ncols = sh.ncols row_list = [] # 从索引为1的地方开始读取,不包含nrows for rownum in range(1,nrows): row = sh.row_values(rownum) if row: for colnum in range(0,ncols): cellvalue = row[colnum].strip() if colnum % 2==1: if not dicEnCnFC.has_key(cellvalue): dicEnCnFC[cellvalue] = row[colnum-1] except: print "read Excel Failed:"+fullconfig+Exception #Get Dataset and FeatureClass,Store in dicAllFC,Key =fc value= ds if os.path.exists(FCDBDir): for dirpath,dirnames,filenames in os.walk(FCDBDir): # 遍历GDB文件夹 获取GDB for dirname in dirnames: if ".gdb" in dirname: gdbfilepath = os.path.join(dirpath,dirname) GDBAllPath.append(gdbfilepath) # 遍历MDB文件夹 获取MDB for filename in filenames: if os.path.splitext(filename)[1]=='.mdb': mdbfilepath = os.path.join(dirpath,filename) GDBAllPath.append(mdbfilepath) # 遍历Shp文件夹 获取Shape for filename in filenames: if os.path.splitext(filename)[1]=='.shp': shpfilepath = os.path.join(dirpath,filename) GDBAllPath.append(dirpath) for everyfilepath in GDBAllPath: env.workspace = everyfilepath singlefclist = arcpy.ListFeatureClasses("","All") if singlefclist and len(singlefclist)>0: for singlefc in singlefclist: # 如果singlefc是unicode则不做改变,否则将utf-8的singlefc编码解码成unicode if not isinstance(singlefc,unicode): singlefc = singlefc.decode('utf-8') # 对于Shape FC会带扩展名.shp,如果是gdb或mdb 则不带 if '.shp' in singlefc: singlefc = singlefc[0:singlefc.find('.shp')] if dicEnCnFC.has_key(singlefc): arcpy.Rename_management(singlefc+".shp",dicEnCnFC[singlefc]+".shp",'') print "Rename "+singlefc+"->"+dicEnCnFC[singlefc]+" Succeed At"+time.strftime("%Y-%m-%d %X",time.localtime()) else: if dicEnCnFC.has_key(singlefc): arcpy.Rename_management(singlefc,dicEnCnFC[singlefc],'') print "Rename "+singlefc+"->"+dicEnCnFC[singlefc]+" Succeed At"+time.strftime("%Y-%m-%d %X",time.localtime()) datasetlist = arcpy.ListDatasets("","Feature") for dataset in datasetlist: # 如果dataset是unicode则不做改变,否则将utf-8的dataset编码解码成unicode if not isinstance(dataset,unicode): dataset = dataset.decode('utf-8') if isinstance(everyfilepath,unicode): env.workspace = everyfilepath+"\\"+dataset dspath = everyfilepath+"\\"+dataset else: env.workspace = everyfilepath+"\\"+dataset.encode('gb2312') dspath = everyfilepath+"\\"+dataset.encode('gb2312') fclist = arcpy.ListFeatureClasses("") if fclist and len(fclist)>0: for fc in fclist: if dicEnCnFC.has_key(fc): arcpy.Rename_management(fc,dicEnCnFC[fc],'') print "Rename "+fc+"->"+dicEnCnFC[fc]+" Succeed At"+time.strftime("%Y-%m-%d %X",time.localtime()) print "Done"