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

来源:互联网 发布:mac装千牛 编辑:程序博客网 时间:2024/04/27 23:00

转载自: http://zhidao.baidu.com/link?url=5mVtLl7qD_FSvAxxe_c2QtjrXspBvIEXd_jJyJF4XEbTEEXTbPC09OHiOK4btxEJvCPE1Bfx1gOGTRiDvCeOrLAsPxYGMB7fQTRv0xap4ea


我稍微改了一下程序

#!/usr/bin/python# -*- coding: utf-8 -*-import rezh_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 matchif __name__ == '__main__':    word1 = 'ceshi,测试'    word2 = 'ceshi,ceshi'    if contain_zh(word1):        print '%s 里面有中文' % word1    if contain_zh(word2):        print '%s 里面有中文' % word2
0 0
原创粉丝点击