运行python脚本,打开文件提示:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb9 in position 0: invalid start byte
解决:
1、查看要读取的文件编码,跟你open(file_dir, 'r', encoding= ) encoding设置的编码一样。
2、修改文件编码,或者修改encoding=
获取文件编码:
# 获取文件编码格式
def get_encoding(self) -> str:
with open(self.file_dir, 'rb') as f:
encoding = chardet.detect(f.read())['encoding']
return encoding