且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

UnicodeDecodeError:'ascii' 编解码器无法解码位置 13 中的字节 0xe2:序号不在范围内(128),关于读取文件

更新时间:2021-12-23 03:40:12

我现在正在上 Python 课,前几天晚上在做涉及文件 IO 的练习时一直遇到同样的问题.如果我使用 IDLE 创建文本文件并将其保存为 .txt 文件而不是 .py ,那不会有问题.我相信这与您用来创建与 python 不兼容的文件的任何程序的编码有关.它很可能会在 python 无法访问的区域中保存诸如 ' 字符之类的东西.我的建议是从 IDLE(或您正在使用的任何程序)启动一个新文件,将您的东西放在那里以创建文件.

I'm in a python class right now and kept running into the same problem the other night when doing exercises involving file IO. It wouldn't be a problem if I were to create the text file using IDLE and saving it as a .txt file instead of .py. I believe it has to do with the encoding of whatever program you are using to create the file not being compatible with python. It's most likely saving things like the ' character in an area that python cant access. My suggestion is to start a new file from IDLE (or whatever program you're using), put your stuff there to create the file.