Python读取文件时出现UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position xx: 解决方案

版权声明:本文为博主原创文章,未经博主允许不得转载 https://blog.csdn.net/zhang__shuang_/article/details/82527314

Python在读取文件时

with open('article.txt') as f:  # 打开新的文本
    text_new = f.read()  # 读取文本数据

出现错误:

UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 145: illegal multibyte sequence

此时有两种解决方案:

1.将第一条语法改为

with open('article.txt','rb') as f:  # 打开新的文本
    text_new = f.read()  # 读取文本数据

2.将第一条语法改为

with open('article.txt','r',encoding='UTF-8') as f:  # 打开新的文本
    text_new = f.read()  # 读取文本数据

猜你喜欢

转载自blog.csdn.net/zhang__shuang_/article/details/82527314