在读取txt文件中遇到问题
UnicodeDecodeError解释为Unicode的解码(decode)出现错误了,也就当前正在处理某种编码类型的字符串,是想要将该字符串去解码,变成Unicode,但是在解码的过程中发生错误了。
解决办法
- 使用windows-1252编码的方式读取txt,open txt文档时加一条encoding=‘windows-1252’
openfile = open('C:\\Users\\86181\\Desktop\\pw.txt', encoding='windows-1252')
- 或者使用utf-8编码,因为txt是utf-8编码
openfile = open('C:\\Users\\86181\\Desktop\\pw.txt',encoding='utf-8')