批量删除GIS数据属性值空格
# -*- coding: utf-8 -*- # --------------------------------------------------------------------------- # Merge.py # Created on: 2015-05-04 10:25:22.00000 # (generated by WangLin_TJCH) # Description: # --------------------------------------------------------------------------- # Import arcpy module import arcpy import os.path import time import random from arcpy import env FCDBDir = "D:\\aaa" dicAllFC={} fcall=[] GDBAllPath=[] if not isinstance(FCDBDir,unicode): FCDBDir = FCDBDir.decode(‘utf-8‘) #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) if not gdbfilepath in GDBAllPath: GDBAllPath.append(gdbfilepath) # 遍历MDB文件夹 获取MDB for filename in filenames: if os.path.splitext(filename)[1]==‘.mdb‘: mdbfilepath = os.path.join(dirpath,filename) if not mdbfilepath in GDBAllPath: GDBAllPath.append(mdbfilepath) # 遍历Shp文件夹 获取Shape for filename in filenames: if os.path.splitext(filename)[1]==‘.shp‘: shpfilepath = os.path.join(dirpath,filename) if not dirpath in GDBAllPath: 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‘) if isinstance(everyfilepath,unicode): fcfullpath = everyfilepath+"\\"+singlefc else: fcfullpath = everyfilepath+"\\"+singlefc.encode(‘gb2312‘) fields = arcpy.ListFields(singlefc) stringfields = [] for everyfield in fields: if everyfield.type == "String": if not everyfield.name in stringfields: stringfields.append(everyfield.name) if len(stringfields)>0: with arcpy.da.UpdateCursor(singlefc, stringfields) as cursor: for row in cursor: for i in range(0,len(stringfields)): #表明属性值有左右有空格 if not isinstance(row[i],unicode): tempunicodestr = str(row[i]) instunicode = tempunicodestr.decode(‘utf-8‘) else: instunicode = row[i] if(instunicode!= instunicode.strip()): row[i] = instunicode.strip() cursor.updateRow(row) print "Delete Space"+fcfullpath+"@"+instunicode+"->"+instunicode.strip()+"@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 isinstance(dataset,unicode): dataset = dataset else: 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: # 如果fc是unicode则不做改变,否则将utf-8的fc编码解码成unicode if isinstance(fc,unicode): fc = fc else: fc = fc.decode(‘utf-8‘) if isinstance(dspath,unicode): fcFullPath = dspath+"\\"+fc else: fcFullPath = dspath+"\\"+fc.encode(‘gb2312‘) env.workspace = fcFullPath randomviewname = fc+"view"+str(random.randint(1,100000)) stringfields = [] fields = arcpy.ListFields(fcFullPath) for everyfield in fields: if everyfield.type == "String": if not everyfield.name in stringfields: stringfields.append(everyfield.name) try: arcpy.MakeFeatureLayer_management(fcFullPath, randomviewname) env.workspace = everyfilepath for i in range(0,len(stringfields)): strWhere = "CHAR_LENGTH("+stringfields[i] +") <> CHAR_LENGTH(TRIM(BOTH ‘ ‘ FROM "+stringfields[i]+"))" arcpy.SelectLayerByAttribute_management(randomviewname, ‘NEW_SELECTION‘,strWhere) with arcpy.da.Editor(env.workspace) as edit: arcpy.CalculateField_management(randomviewname,stringfields[i],"!"+stringfields[i]+"!.strip()", ‘PYTHON‘) print "Delete Space"+fcFullPath+" Succeed At"+time.strftime("%Y-%m-%d %X",time.localtime()) except arcpy.ExecuteError: print(arcpy.GetMessages(2)) else: print "Dir Not Exist" print "Done"
时间: 2024-11-18 16:18:54