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

一、摘要

使用 xlrd 模块打开带中文的excel文件时,会报错。

FileNotFoundError: [Errno 2] No such file or directory: 'xx.xlsx'

 

这个时候,就需要检测文件名,是否包含中文,及时return。

 

二、原理

中文字符的编码范围是:

\u4e00 - \u9fff

只要编码在此范围就可判断为中文字符

 

三、函数

def is_chinese(self, string):
    """
    检查整个字符串是否包含中文
    :param string: 需要检查的字符串
    :return: bool
    """
    for ch in string:
        if u'\u4e00' <= ch <= u'\u9fff':
            return True

    return True

 

转自:https://www.cnblogs.com/xiao987334176/p/11239982.html

posted @ 2020-06-06 23:29  阿布_alone  阅读(3259)  评论(0编辑  收藏  举报
TOP