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() # 读取文本数据

05-11 17:32