搬运地址: http://luopuya.github.io/2014/03/29/Python 判断汉字字符/
为了清洗数据, 项目中需要去除所有不包含中文的字符串
使用 unicode 范围 \u4e00 - \u9fff 来判别汉字
unicode 分配给汉字(中日韩越统一表意文字)的范围为 4E00-9FFF
(目前 unicode 6.3 的标准已定义到 9FCC )
在 Python 3 中,判断字符是否汉字的方法如下:
def ishan(text):
# for python 3.x
# sample: ishan('一') == True, ishan('我&&你') == False
return all('\u4e00' <= char <= '\u9fff' for char in text)
Python 2写法
def ishan(text):
# for python 2.x, 3.3+
# sample: ishan(u'一') == True, ishan(u'我&&你') == False
return all(u'\u4e00' <= char <= u'\u9fff' for char in text)
补充说明
- Python 3.3+ 重新支持 Python 2 中用 u 表示 unicode 的方式
- \u4e00-\u9fff 不包含中文符号,如有需要可参考维基
- 网上常见的 \u4e00-\u9fa5 写太死了,虽说目前而言后面的都是极其罕见的字