// 读取txt文件
with open(file_path + filename, "r", encoding='utf-8') as f:
content = f.readlines()
print(content)
报错:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xbd in position 0: invalid start byte
解决办法
1.先将 encoding=‘utf-8’ 改为 encoding=“unicode_escape” ,运行
with open(file_path + filename, "r", encoding="unicode_escape") as f:
content = f.readlines()
2.然后将 encoding=“unicode_escape” 改为 encoding=“gbk” ,再运行即可。
with open(file_path + filename, "r", encoding='gbk') as f:
content = f.readlines()
print(content)