Python根据unicode判断语言类型

 

 

 1 def is_chinese(uchar):
 2 """判断一个unicode是否是汉字"""
 3     if uchar >= u'\u4e00' and uchar<=u'\u9fa5':
 4         return True
 5     else:
 6         return False
 7  
 8 def is_number(uchar):
 9 """判断一个unicode是否是数字"""
10     if uchar >= u'\u0030' and uchar<=u'\u0039':
11         return True
12     else:
13         return False
14  
15 def is_alphabet(uchar):
16 """判断一个unicode是否是英文字母"""
17     if (uchar >= u'\u0041' and uchar<=u'\u005a') or (uchar >= u'\u0061' and uchar<=u'\u007a'):
18         return True
19     else:
20         return False
21  
22 def is_other(uchar):
23 """判断是否非汉字,数字和英文字符"""
24     if not (is_chinese(uchar) or is_number(uchar) or is_alphabet(uchar)):
25         return True
26     else:
27         return False
28  
29 def B2Q(uchar):
30 """半角转全角"""
31     inside_code=ord(uchar)
32     if inside_code<0x0020 or inside_code>0x7e: #不是半角字符就返回原来的字符
33         return uchar
34     if inside_code==0x0020: #除了空格其他的全角半角的公式为:半角=全角-0xfee0
35         inside_code=0x3000
36     else:
37         inside_code+=0xfee0
38     return unichr(inside_code)
39  
40 def Q2B(uchar):
41 """全角转半角"""
42     inside_code=ord(uchar)
43     if inside_code==0x3000:
44         inside_code=0x0020
45     else:
46         inside_code-=0xfee0
47     if inside_code<0x0020 or inside_code>0x7e: #转完之后不是半角字符返回原来的字符
48         return uchar
49     return unichr(inside_code)
50  
51 def stringQ2B(ustring):
52 """把字符串全角转半角"""
53     return "".join([Q2B(uchar) for uchar in ustring])
54  
55 def uniform(ustring):
56 """格式化字符串,完成全角转半角,大写转小写的工作"""
57     return stringQ2B(ustring).lower()
58  
59 def string2List(ustring):
60 """将ustring按照中文,字母,数字分开"""
61 retList=[]
62 utmp=[]
63 for uchar in ustring:
64 if is_other(uchar):
65 if len(utmp)==0:
66 continue
67 else:
68 retList.append("".join(utmp))
69 utmp=[]
70 else:
71 utmp.append(uchar)
72 if len(utmp)!=0:
73 retList.append("".join(utmp))
74 return retList

https://blog.csdn.net/luxiangzhou/article/details/83651623

转载;https://blog.csdn.net/uestcyao/article/details/22092403?utm_source=blogxgwz1

posted @ 2020-12-26 16:22  Σωκράτης  阅读(207)  评论(0)    收藏  举报