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

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

删除GIS数据属性值空格(GDB,MDB,Shp)的相关文章

批量裁剪GIS数据(包含GDB,MDB,Shp)

# -*- coding: utf-8 -*- # made by 汪林_质检处 import os.path import arcpy import sys from arcpy import env FCDBDir = "E:\\cliptest\\data" output = "E:\\Result" clipshp = "E:\\cliptest\\clip.shp" GDBAllPath=[] # OID字段名称 ShapeOID =

删除GIS数据库空层(GDB,MDB,Shape)

批量删除GIS数据库空层. # -*- coding: utf-8 -*- # --------------------------------------------------------------------------- # Merge.py # Created on: 2013-01-21 10:25:22.00000 # (generated by WangLin_TJCH) # Description: # ------------------------------------

已更新或删除的行值要么不能使该行成为唯一行,要么改变了多个行

在对一个表做试验的时候,往里面添加了几条数据,后来发现原本想作为主键的字段的值都为"NULL",这可不行,删除重新来吧.选中删除的时候就出现了 "已更改或删除的行值要么不能使改行成为唯一行,要么改变了多个行(X行)"的错误. 原来是建表的时候忘了添加主键,但是现在删除也删除不了,往里面填东西吧,也填不了,也报错.没有办法啊~百度吧~各种解决办法,终于找到了我这个菜鸟能看懂而且操作出来的方法.(好方法!!!) 解决方法: 单击SQL工具栏上的"新建查询&qu

返回数组中的最大值和删除数组重复值-排序

//数组中最大值function getMax(arr){ //取该数组第一个值为最大值 var max=arr[0]; for(var i=0;i<arr.length;i++){ if(arr[i]>max){ max=arr[i] } } return max;} console.log(getMax([2,98,10,88])) /*--------------------------------------------------------------------*///删除数组重

【sql server】&quot;已更新或删除的行值要么不能使该行成为唯一行,要么改变了多个行&quot; 解决方案

#事故现场: 1.在手动修改某表中数据是,出现如下错误提示:  已更新或删除的行值要么不能使该行成为唯一行,要么改变了多个行 2.表结构及数据: #解决方法: 1.原因分析:提示被删除的行不是唯一行,检查发现: 表中出现了重复的数据({name:小红,age:10}),所以当视图删除时,会提示错误: 2.解决方法一:添加中间,使行值成为唯一值: sql语句: ALTER TABLE UserInfo add ID int identity primary key 效果: 3.解决方法二:先将原用

删除deviceToken里的空格

preg_replace("/\s/","",$deviceToken);//删除deviceToken里的空格 原文地址:https://www.cnblogs.com/ycqi/p/11423252.html

关于gdb和shp的FID问题

gdb的FID从1开始,并且FID唯一,从数字化时开始,每个图形对应唯一的FID,删除图形亦删除对应的FID.FID可能出现中断的情况. shp的FID从0开始,并且永远连续.删除图形,则编号在其下面的图形,会相应上移.

每天一道算法题(35)——删除字符串首尾的空格

题目: 输入字符串,删除首尾的空格.单词中间只留下一个空格. 如输入"   a    b    c   "输出"a b c" 代码: public static String test1(String s) { StringBuffer sb=new StringBuffer(s.trim()); int j=0; int i; for(;j<sb.length();){ i=j; while(sb.charAt(j)==' '&&j<s

如何更快的删除String中的空格[未完]

  背景:此文章主要源于网址[1]所描述的,文中大部分方法亦是[1]中实现的. 下面介绍集中删除空格的方法: 方法1:按空格分割后再拼接 /// <summary> /// 按空格分割后拼接--Join /// </summary> /// <param name="str"></param> /// <returns></returns> public static string TrimAllWithSplit