To determine if a character is Chinese
string’scoding should be unicode
to know if one character is Chinese
we can decode utf-8 to unicode
def is_chinese(uchar):
"""判断一个unicode是否是汉字"""
if uchar >= u'\u4e00' and uchar<=u'\u9fa5':
return True
else:
return False
In Python, convert utf-8 to unicode
string.decode('utf-8')
convert unicode to utf-8
string.encode('utf-8')
for i in '下:@uVT4HLJLA: 二、我是用MAC的,所以可以骂你脑残'.decode('utf-8'):
print i, is_chinese(i)
下 True
: False
@ False
u False
V False
T False
4 False
H False
L False
J False
L False
A False
: False
False
二 True
、 False
我 True
是 True
用 True
M False
A False
C False
的 True
, False
所 True
以 True
可 True
以 True
骂 True