python3读取中文文件出现UnicodeDecodeError: ‘gbk’ codec can’t decode byte 0xff in position

版权声明:转载请标明出处 https://blog.csdn.net/easy_purple/article/details/82821169

本想打开中文的txt文本文件,

with open(r'C:/Users/dell/Desktop/26844.txt', errors='ignore') as f:
    text = f.read()

报错‘gbk’ codec can’t decode byte 0xff in position 5657: illegal multibyte sequence”

解决办法:指定编码格式

with open(r'C:/Users/dell/Desktop/26844.txt', encoding="gbk") as f:
    text = f.read()

依旧报错:UnicodeDecodeError: ‘gbk’ codec can’t decode byte 0xff in position 5657: illegal multibyte sequence

解决办法:ignore忽略

with open(r'C:/Users/dell/Desktop/26844.txt', encoding="gbk",errors='ignore') as f:
    text = f.read()

ok了

猜你喜欢

转载自blog.csdn.net/easy_purple/article/details/82821169