要素类属性内容全角换半角

近期天地图项目处理数据时由于原始数据质量较差,属性字段中多有全角字符,带来后期处理与开发不便,因此做了个属性全角换半角的脚本如下

经验:Unicode处理

# -*- coding: utf-8 -*-

__author__ = 'tanshuai'

import arcpy

from arcpy import env

fullCorner = ['0' , '1' , '2' , '3' , '4' ,

'5' , '6' , '7' , '8' , '9' ,

'A' , 'B' , 'C' , 'D' , 'E' ,

'F' , 'G' , 'H' , 'I' , 'J' ,

'K' , 'L' , 'M' , 'N' , 'O' ,

'P' , 'Q' , 'R' , 'S' , 'T' ,

'U' , 'V' , 'W' , 'X' , 'Y' ,

'Z' , 'a' , 'b' , 'c' , 'd' ,

'e' , 'f' , 'g' , 'h' , 'i' ,

'j' , 'k' , 'l' , 'm' , 'n' ,

'o' , 'p' , 'q' , 'r' , 's' ,

't' , 'u' , 'v' , 'w' , 'x' ,

'y' , 'z' , '-' , ' ' , ':' ,

'.' , ',' , '/' , '%' , '#' ,

'!' , '@' , '&' , '(' , ')' ,

'<' , '>' , '"' , ''' , '?' ,

'[' , ']' , '{' , '}' , '\' ,

'|' , '+' , '=' , '_' , '^' ,

'¥' , ' ̄' , '`']

halfCorner = ['0', '1', '2', '3', '4',

'5', '6', '7', '8', '9',

'A', 'B', 'C', 'D', 'E',

'F', 'G', 'H', 'I', 'J',

'K', 'L', 'M', 'N', 'O',

'P', 'Q', 'R', 'S', 'T',

'U', 'V', 'W', 'X', 'Y',

'Z', 'a', 'b', 'c', 'd',

'e', 'f', 'g', 'h', 'i',

'j', 'k', 'l', 'm', 'n',

'o', 'p', 'q', 'r', 's',

't', 'u', 'v', 'w', 'x',

'y', 'z', '-', ' ', ':',

'.', ',', '/', '%', '#',

'!', '@', '&', '(', ')',

'<', '>', '"', '\'','?',

'[', ']', '{', '}', '\\',

'|', '+', '=', '_', '^',

'¥','~', '`']

def replaceCharacter(fldValueText):

    for full in fullCorner:

        if fldValueText.find(full) > -1:

            index = fullCorner.index(full)

            half = halfCorner[index];

            fldValueText = fldValueText.replace(full, half)

    print fldValueText

    return fldValueText

env.workspace = "E:\\test2"

length = len(fullCorner)

cursor = arcpy.UpdateCursor("test3.shp")

try:

    for row in cursor:

        fieldList = arcpy.ListFields("test3.shp")

        for field in fieldList:

            if field.type == "String":

                value = row.getValue(field.name)

                valueTxt = value.encode('utf-8')

                if len(valueTxt) > 0:

                    print valueTxt

                    resultValue = replaceCharacter(valueTxt)

                    row.setValue(field.name, resultValue)

                    cursor.updateRow(row)

                    print resultValue

                else:

                    continue

except Exception,e:

    print("错误"+e)

finally:

    del row,cursor

print("complete!")

原文地址:https://www.cnblogs.com/Brune/p/3113119.html