您的位置:首页 > 编程语言 > Python开发

【python脚本收集】全角半角转换

2016-01-25 21:36 706 查看
1.全角转半角

def strQ2B(ustring):
"""把字符串全角转半角"""
rstring = ""
for uchar in ustring:
inside_code=ord(uchar)
if inside_code==0x3000:
inside_code=0x0020
else:
inside_code-=0xfee0
if inside_code<0x0020 or inside_code>0x7e: #转完之后不是半角字符返回原来的字符
rstring += uchar
rstring += unichr(inside_code)
return rstring
2.半角转全角

def strB2Q(ustring):
"""把字符串半角转全角"""
rstring = ""
for uchar in ustring:
inside_code=ord(uchar)
if inside_code<0x0020 or inside_code>0x7e: #不是半角字符就返回原来的字符
rstring += uchar
if inside_code==0x0020: #除了空格其他的全角半角的公式为:半角=全角-0xfee0
inside_code=0x3000
else:
inside_code+=0xfee0
rstring += unichr(inside_code)
return rstring
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: