您的位置:首页 > 编程语言 > Python开发

Python 判断字符串中是否包含中文

2016-10-12 22:38 555 查看
转载自: http://zhidao.baidu.com/link?url=5mVtLl7qD_FSvAxxe_c2QtjrXspBvIEXd_jJyJF4XEbTEEXTbPC09OHiOK4btxEJvCPE1Bfx1gOGTRiDvCeOrLAsPxYGMB7fQTRv0xap4ea

我稍微改了一下程序

#!/usr/bin/python
# -*- coding: utf-8 -*-

import re

zh_pattern = re.compile(u'[\u4e00-\u9fa5]+')

def contain_zh(word):
'''
判断传入字符串是否包含中文
:param word: 待判断字符串
:return: True:包含中文  False:不包含中文
'''
word = word.decode()
global zh_pattern
match = zh_pattern.search(word)

return match

if __name__ == '__main__':
word1 = 'ceshi,测试'
word2 = 'ceshi,ceshi'

if contain_zh(word1):
print '%s 里面有中文' % word1
if contain_zh(word2):
print '%s 里面有中文' % word2
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python