python处理编码小工具集合[整理篇]
时间:2010-06-27 来源:hkebao
1.python字符全角半角转换
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
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
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
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
相关阅读 更多 +