Remove GIS data attribute value spaces (GDB,MDB,SHP)

Source: Internet
Author: User

Bulk Delete GIS data attribute value spaces

#-*-coding:utf-8-*-# ---------------------------------------------------------------------------#merge.py#Created on:2015-05-04 10:25:22.00000#(generated by wanglin_tjch)#Description:# ---------------------------------------------------------------------------#Import arcpy ModuleImportarcpyImportOs.pathImport TimeImportRandom fromarcpyImportEnvfcdbdir="d:\\aaa"DICALLFC={}fcall=[]gdballpath=[]if  notisinstance (fcdbdir,unicode): Fcdbdir= Fcdbdir.decode ('Utf-8')#Get Dataset and Featureclass,store in Dicallfc,key =fc value= DSifos.path.exists (fcdbdir): forDirpath,dirnames,filenamesinchOs.walk (fcdbdir):#Traverse gdb folder to get GDB         forDirNameinchDirnames:if ". GDB" inchDirname:gdbfilepath=Os.path.join (dirpath,dirname)if  notGdbfilepathinchGDBAllPath:GDBAllPath.append (Gdbfilepath)#Iterate MDB folder get MDB         forFileNameinchFilenames:ifOs.path.splitext (filename) [1]=='. mdb': Mdbfilepath=Os.path.join (dirpath,filename)if  notMdbfilepathinchGDBAllPath:GDBAllPath.append (Mdbfilepath)#Traverse shp folder to get shape         forFileNameinchFilenames:ifOs.path.splitext (filename) [1]=='. SHP': Shpfilepath=Os.path.join (dirpath,filename)if  notDirpathinchGDBAllPath:GDBAllPath.append (Dirpath) forEveryfilepathinchGDBAllPath:env.workspace=Everyfilepath singlefclist= arcpy. Listfeatureclasses (""," All")        ifSinglefclist andLen (singlefclist) >0: forSinglefcinchsinglefclist:#if SINGLEFC is Unicode then do not change, otherwise utf-8 SINGLEFC encoding is decoded to Unicode                if  notisinstance (singlefc,unicode): SINGLEFC= Singlefc.decode ('Utf-8')                ifisinstance (everyfilepath,unicode): Fcfullpath= everyfilepath+"\\"+SINGLEFCElse: Fcfullpath= everyfilepath+"\\"+singlefc.encode ('gb2312') Fields=arcpy. ListFields (SINGLEFC) Stringfields= []                 forEveryfieldinchFields :ifEveryfield.type = ="String":                        if  notEveryfield.nameinchstringfields:stringfields.append (everyfield.name)ifLen (stringfields) >0:with arcpy.da.UpdateCursor (SINGLEFC, stringfields) as cursor: forRowinchcursor: forIinchRange (0,len (stringfields)):#indicates that there are spaces around the property value                                if  notisinstance (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")         forDataSetinchdatasetlist:#If the dataset is Unicode, it does not change, otherwise the Utf-8 dataset encoding is decoded to Unicode            ifisinstance (dataset,unicode): DataSet=DataSetElse: DataSet= Dataset.decode ('Utf-8')            ifisinstance (everyfilepath,unicode): Env.workspace= everyfilepath+"\\"+DataSet Dspath= everyfilepath+"\\"+DataSetElse: Env.workspace= everyfilepath+"\\"+dataset.encode ('gb2312') Dspath= everyfilepath+"\\"+dataset.encode ('gb2312') Fclist= arcpy. Listfeatureclasses ("")            ifFclist andLen (fclist) >0: forFcinchfclist:#If FC is Unicode then do not change, otherwise Utf-8 FC encoding is decoded to Unicode                    ifisinstance (fc,unicode): FC=FCElse: FC= Fc.decode ('Utf-8')                    ifisinstance (dspath,unicode): Fcfullpath= dspath+"\\"+FCElse: Fcfullpath= dspath+"\\"+fc.encode ('gb2312') Env.workspace=Fcfullpath Randomviewname= fc+"View"+str (Random.randint (1,100000)) Stringfields=[] fields=arcpy. ListFields (Fcfullpath) forEveryfieldinchFields :ifEveryfield.type = ="String":                            if  notEveryfield.nameinchstringfields:stringfields.append (everyfield.name)Try: arcpy. Makefeaturelayer_management (Fcfullpath, randomviewname) env.workspace=Everyfilepath forIinchRange (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:arc Py. Calculatefield_management (Randomviewname,stringfields[i],"!"+stringfields[i]+"!. Strip ()",'PYTHON')                                Print "Delete Space"+fcfullpath+"succeed at"+time.strftime ("%y-%m-%d%x", Time.localtime ())exceptarcpy. Executeerror:Print(arcpy. Getmessages (2))                        Else:    Print "Dir not Exist"Print " Done"
View Code

Remove GIS data attribute value spaces (GDB,MDB,SHP)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.