跳到主要内容

半角全角转换

https://www.cnblogs.com/kaituorensheng/p/3554571.html

from six import unichr


def strQ2B(ustring):
"""全角转半角"""
rstring = ""
for uchar in ustring:
inside_code = ord(uchar)
if inside_code == 12288: # 全角空格直接转换
inside_code = 32
elif 65281 <= inside_code <= 65374: # 全角字符(除空格)根据关系转化
inside_code -= 65248

rstring += unichr(inside_code)
return rstring


def strB2Q(ustring):
"""半角转全角"""
rstring = ""
for uchar in ustring:
inside_code = ord(uchar)
if inside_code == 32: # 半角空格直接转化
inside_code = 12288
elif 32 <= inside_code <= 126: # 半角字符(除空格)根据关系转化
inside_code += 65248

rstring += unichr(inside_code)
return rstring


b = strQ2B("mn123abc博客园")
print(b)

c = strB2Q("mn123abc博客园")
print(c)