问题
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