python如何判断一个字符串是中文,还是英文。

参考链接:
https://blog.csdn.net/hit0803107/article/details/52885702

 

 

decode:  将其它编码转成  ===》unicode

encode:  将 unicode   ====》其它编码

 

 

 

#-*- coding:utf-8 -*-
#python 判断字符串是中文还是英文,只要有一个中文就算中文。


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('i love yiu ')
print check_contain_chinese('i love you')
print check_contain_chinese('xx中国')


 

posted @ 2018-08-27 16:37  yoyoma0355  阅读(27756)  评论(0编辑  收藏  举报