2

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
Kara
  • 6,115
  • 16
  • 50
  • 57
Qman485
  • 115
  • 2
  • 9
  • Could you post some code you've tried so far? – JRodDynamite Oct 23 '15 at 04:51
  • 2
    Possible duplicate of [UnicodeDecodeError: 'ascii' codec can't decode byte 0xef in position 1](http://stackoverflow.com/questions/10561923/unicodedecodeerror-ascii-codec-cant-decode-byte-0xef-in-position-1) – l'L'l Oct 23 '15 at 04:53
  • @l'L'l so it could just be my terminal being weird? – Qman485 Oct 23 '15 at 04:58
  • 1
    You likely need to decode whatever it is with a different codec besides `ascii`.... (eg. `str.decode('utf-8')`) – l'L'l Oct 23 '15 at 05:02
  • @Qman485: This question/answer is similar/relevant to yours: http://stackoverflow.com/questions/6048085/python-write-unicode-text-to-a-text-file – l'L'l Oct 23 '15 at 05:05
  • @l'L'l Im sure my prof will do that, thanks so much! Take care :) – Qman485 Oct 23 '15 at 05:07
  • The last line of code of the chosen correct answer is going to be what you want. :) cheers! – l'L'l Oct 23 '15 at 05:07
  • You're terminal probably is using a fancy apostrophy such as http://www.fileformat.info/info/unicode/char/2019/index.htm which is not ascii. https://stackoverflow.com/questions/983720/python-3-doesnt-read-unicode-file-on-a-new-server explains how to deal with non-ascii characters. The previous duplicate is not terribly accurate since this is python3.x which deals with encoding differently than python2.x – Jonathan Villemaire-Krajden Oct 23 '15 at 05:13
  • @JonathanVillemaire-Krajden but would it compile and work on other computers? – Qman485 Oct 23 '15 at 05:47

1 Answers1

0

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.

sstoxen
  • 162
  • 1
  • 7
  • yea i am on a mac and I'm using text edit, the weird thing is that it works for every file, every file that I was assigned didnt have an 's but when I tried it on my own with them it crashes, I don't want to loose marks :/ EDIT: just tried it on idle and it worked.... Thats so weird but thank you so much, made me feel a bit better haha ! – Qman485 Oct 23 '15 at 05:05
  • awesome! ya I was double checking my work and then all of a sudden was like "What is this!" haha, got me scared. I think it should all be fine. I will talk to my prof tomorrow though. Thanks so much! – Qman485 Oct 23 '15 at 05:55