在读取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')
博客主要讲述了Python读取txt文件时遇到的UnicodeDecodeError问题,即Unicode解码出错。解决办法有两种,一是使用windows - 1252编码方式读取,在open txt文档时添加encoding='windows - 1252';二是使用utf - 8编码,因为txt是utf - 8编码。
1129

被折叠的 条评论
为什么被折叠?



