UnicodeDecodeError:'ascii' 编解码器无法解码位置 13 中的字节 0xe2:序号不在范围内(128),关于读取文件 [英] UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 13: ordinal not in range(128), regarding reading in files

查看:41
本文介绍了UnicodeDecodeError:'ascii' 编解码器无法解码位置 13 中的字节 0xe2:序号不在范围内(128),关于读取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我总是收到此错误 UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 13: ordinal not in range(128) 当我尝试将文件读入我的 python 时具有 's 的程序.例如,It's"这个词会使我的程序崩溃,我会得到这个错误.它为什么这样做?

I always get this error UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 13: ordinal not in range(128) when ever I try to read in a file to my python program that has an 's. For example the word "It's" would crash my program and I would get this error. Why does it do this?

def readInFile(fileName):
    inputFile = open(fileName, 'r')
    SomeInput = inputFile.read()
    inputFile.close()
    return SomeInput

推荐答案

我现在正在上 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.

这篇关于UnicodeDecodeError:'ascii' 编解码器无法解码位置 13 中的字节 0xe2:序号不在范围内(128),关于读取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆