码迷,mamicode.com
首页 > 数据库 > 详细

删除GIS数据属性值空格(GDB,MDB,Shp)

时间:2016-01-07 16:04:19      阅读:270      评论:0      收藏:0      [点我收藏+]

标签:

批量删除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"
View Code

 

删除GIS数据属性值空格(GDB,MDB,Shp)

标签:

原文地址:http://www.cnblogs.com/514687800/p/5109855.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!