一、摘要
使用 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