UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 13: ordinal not in range(128), regarding reading in files

こ雲淡風輕ζ 提交于 2019-12-11 13:16:00

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/33295085/unicodedecodeerror-ascii-codec-cant-decode-byte-0xe2-in-position-13-ordinal

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!