首页 > 编程语言 >程序报错:UnicodeDecodeError: 'gbk' codec can't decode byte 0xad in position 2: illegal mu

程序报错:UnicodeDecodeError: 'gbk' codec can't decode byte 0xad in position 2: illegal mu

时间:2022-08-31 10:03:40浏览次数:64  
标签:读取 encoding sequence multibyte 报错 file open

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

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了

标签:读取,encoding,sequence,multibyte,报错,file,open
From: https://www.cnblogs.com/yin-qiu-moon/p/16641948.html

相关文章