python中半角与全角互相转换

半角与全角的转换,代码如下:

# -*- coding: cp936 -*-
def strQ2B(ustring):
    """全角转半角"""
    rstring = ""
    for uchar in ustring:
        inside_code=ord(uchar)
        if inside_code == 12288:                              #全角空格直接转换            
            inside_code = 32 
        elif (inside_code >= 65281 and 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 inside_code >= 32 and inside_code <= 126:        #半角字符(除空格)根据关系转化
            inside_code += 65248

        rstring += unichr(inside_code)
    return rstring



b = strQ2B("mn123abc博客园".decode('cp936'))                           
print b

c = strB2Q("mn123abc博客园".decode('cp936'))                           
print c


你可能感兴趣的:(python,python,转换,全角,半角)