程序报错:UnicodeDecodeError: 'gbk' codec can't decode byte 0xad in position 2: illegal multibyte sequence,解决方法

当我输入代码,读取文件的时候

file=open('a.txt','r')
print(file.readlines())
file.close()

结果报这个错:

 

 一看,发现编码出错了,百度了一下,原来open函数其中的encoding参数的默认值是None是不能读取中文字符的,所以要给encoding参数重新传入值才能读取中文字符。

修改后的代码:

file=open('a.txt','r',encoding='UTF-8')
print(file.readlines())
file.close()

这样结果就OK了

posted @ 2022-08-31 09:50  訫語  阅读(233)  评论(0)    收藏  举报