#全角轉半角
def strQ2B(featureClass):
#把字符串全角轉半角
rows=arcpy.UpdateCursor(featureClass)
for row in rows:
rstring=""
ustring=row.NAME_CHN
for uchar in ustring:
inside_code=ord(uchar)
#全角空格爲12288,半角爲32
if inside_code==12288:
inside_code=32
else:
inside_code-=65248
#轉完之後不是半角字符返回原來的字符
if inside_code<32 or inside_code>126:
rstring+=uchar
else:
rstring+=unichr(inside_code)
row.NAME_CHN=rstring
rows.updateRow(row)
del row
del rows
在網上找了很多相關的例子,但是在處理featureClass時候,效果很不理想,可能是由於arcgis只識別gb18030編碼的原因,因此,重新寫了一個全角轉半角的方法。
這個會修改源數據,所以想要直接拿來用的話,最好先備份一下源數據。
def strQ2B(ustring):
"""把字符串全角轉半角"""
rstring = ""
for uchar in ustring:
inside_code=ord(uchar)
#全角空格爲12288,半角爲32
if inside_code==12288:
inside_code=32
else:
inside_code-=65248
#轉完之後不是半角字符返回原來的字符
if inside_code<32 or inside_code>126:
rstring+=uchar
else:
rstring+=unichr(inside_code)
return rstring