python读取txt文件报错:UnicodeDecodeError: 'utf8' codec can't decode byte 0xb3 in position 0的解决方法

时间:2021-06-29 20:52:41

1、python3在读txt文件时出现UnicodeDecodeError: 'utf8' codec can't decode byte 0xb3 in position 0错误

fr = codecs.open('test100.txt', 'r', 'utf-8')
解决方法:把utf-8改为gbk编码

fr = codecs.open('test100.txt', 'r', 'gbk')即可解决这个问题
参考链接:https://blog.csdn.net/qingyuanluofeng/article/details/45190867

2、UnicodeDecodeError: 'utf-8' codec can't decode byte 0xbb in position 8: invalid start byte

通过方法一之后还是无法解决

终极解决方法:

fr = codecs.open(filename, 'rb', 'utf-8','ignore')

亲测可用!

参考链接:https://blog.csdn.net/u012883674/article/details/78478874