import chardet # 首先二进制方式打开文件 with open(absPath, 'rb') as frb: # 检测编码方式 cur_encoding = chardet.detect(frb.read())['encoding'] # 指定文件编码方式 with open(absPath, 'r', encoding=cur_encoding) as fr: Content = fr.read()