python 字符串全角和半角相互转换

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 
        else: 
            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:  
            inside_code=0x3000 
        else: 
            inside_code+=0xfee0 
        rstring += unichr(inside_code) 
    return rstring

绝世唐门www.zuoann.com莽荒纪www.42shu.com

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