1.全角转半角
1.1字符串全角转半角
# coding:utf-8
from idna import unichr
def all_to_half(all_string):
"""全角转半角"""
half_string = ""
for char in all_string:
inside_code = ord(char)
if inside_code == 12288: # 全角空格直接转换,全角和半角的空格的Unicode值相差12256
inside_code = 32
elif (inside_code >= 65281 and inside_code <= 65374): # 全角字符(除空格)根据关系转化,除空格外的全角和半角的Unicode值相差65248
inside_code -= 65248
half_string += unichr(inside_code)
return half_string
1.2单个字符 全角转半角
def Q2B(uchar):
"""单个字符 全角转半角"""
inside_code = ord(uchar)
if inside_code == 0x3000:
inside_code = 0x0020
else:
inside_code -= 0xfee0
if inside_code < 0x0020 or inside_code > 0x7e: #转完之后不是半角字符返回原来的字符
return uchar
return chr(inside_code)
1.3单个字符 半角转全角
def B2Q(uchar):
"""单个字符 半角转全角"""
inside_code = ord(uchar)
if inside_code < 0x0020 or inside_code > 0x7e: # 不是半角字符就返回原来的字符
return uchar
if inside_code == 0x0020: # 除了空格其他的全角半角的公式为: 半角 = 全角 - 0xfee0
inside_code = 0x3000
else:
inside_code += 0xfee0
return chr(inside_code)
2.判断
2.1汉字的判断
汉字的unicode编码范围 u4e00 到 u9fa5。
def is_chinese(uchar):
"""判断一个unicode是否是汉字"""
if uchar >= u'\u4e00' and uchar<=u'\u9fa5':
return True
else:
return False
2.2数字0-9的判断
数字的unicode编码范围根据全角和半角,有两个不同区域,半角数字 u0030 到 u0039,全角数字 uff10 到 uff19。
def is_number(uchar):
"""判断一个unicode是否是半角数字"""
if uchar >= u'\u0030' and uchar<=u'\u0039':
return True
else:
return False
def is_Qnumber(uchar):
"""判断一个unicode是否是全角数字"""
if uchar >= u'\uff10' and uchar <= u'\uff19':
return True
else:
return False
2.3大小写字母判断
字母的unicode编码根据字母大小写,以及全角和半角共有四个区域。
半角大写字母:u0041 - u005a ,半角小写字母:u0061 - u007a ;
全角大写字母:uff21 - uff3a , 全角小写字母:uff41 - uff5a 。
def is_alphabet(uchar):
"""判断一个unicode是否是半角英文字母"""
if (uchar >= u'\u0041' and uchar <= u'\u005a') or (uchar >= u'\u0061' and uchar <= u'\u007a'):
return True
else:
return False
def is_Qalphabet(uchar):
"""判断一个unicode是否是全角英文字母"""
if (uchar >= u'\uff21' and uchar <= u'\uff3a') or (uchar >= u'\uff41' and uchar <= u'\uff5a'):
return True
else:
return False
2.4非汉字和数字字母的判断
判断除汉字、数字0-9、字母之外的字符。
def is_other(uchar):
"""判断是否非汉字,数字和英文字符"""
if not (is_chinese(uchar) or is_number(uchar) or is_alphabet(uchar)):
return True
else:
return False