19、判断是否包含中文
<p>python2下</p>
<pre><code>#-*- coding:utf-8 -*-
import sys
reload(sys)
sys.setdefaultencoding('utf8')
def check_contain_chinese(check_str):
for ch in check_str.decode('utf-8'):
if u'\u4e00' <= ch <= u'\u9fff':
return True
return False
if __name__ == "__main__":
print check_contain_chinese('中国')
print check_contain_chinese('xxx')
print check_contain_chinese('xx中国')
结果:
True
False
True</code></pre>
<p>Python3下</p>
<pre><code>#判断一段文本中是否包含简体中文
import re
zhmodel = re.compile(u'[\u4e00-\u9fa5]') #检查中文
#zhmodel = re.compile(u'[^\u4e00-\u9fa5]') #检查非中文
contents = u'(2014)深南法民二初字第280号'
match = zhmodel.search(contents)
if match:
print(contents)
else:
print(u'没有包含中文')
</code></pre>