Weird Unicode error

Nilats (3750 points)
7 14 29
asked Nov 25, 2021 in HW6 by Nilats (3,750 points)

I get this weird error when I try to open the file "example.txt"

"UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe8 in position 5862: invalid continuation byte"

there are only numbers and commas inside the file.


5 Answers

Best answer
GabrielAlexandru (7760 points)
2 4 29
answered Nov 26, 2021 by GabrielAlexandru (7,760 points)
selected Dec 3, 2021 by Nilats
People in the Italian course had the same problem. In their case it was related to the fact that they were using the file. When they switched to the everything worked fine.

I don’t know if this applies to you too, but you can try!
andrea.sterbini (172780 points)
513 935 1789
commented Nov 26, 2021 by andrea.sterbini (172,780 points)

Have you tried to add the following line at the top of the file?

# -*- coding: utf-8 -*-

Nilats (3750 points)
7 14 29
commented Nov 26, 2021 by Nilats (3,750 points)
Yeah, I just copy-pasted the whole code to the English file and now it's working.
nane.harutyunyan (790 points)
0 0 4
answered Nov 25, 2021 by nane.harutyunyan (790 points)

I am not very sure that it has anything to do with this, but maybe try to specify the encoding explicitly when opening the file (in case you haven't done so); pass encoding="utf-8" in the opening function.

Nilats (3750 points)
7 14 29
commented Nov 25, 2021 by Nilats (3,750 points)
I've already done it.
I also tried encoding="latin-1"
rokshana03 (7140 points)
1 3 27
answered Nov 26, 2021 by rokshana03 (7,140 points)
So, I tried to do some research about this. In most of the cases, they say to change the encoding to "latin-1" but it doesn't work for you.

You could try with encoding ='ISO-8859-1' or by opening it in "rb" so in binary mode. ( this is what internet sources say)                                                                                                                  

Although, I'm wondering how you got that error. The file itself is encoded in utf-8, I opened it just fine. i doubt that its encoded in another format. Did you remove the spaces before opening it? Perhaps, something you did modified the encoding of the file?
Nilats (3750 points)
7 14 29
commented Nov 26, 2021 by Nilats (3,750 points)
I already tried those as well and nope, it's not working.
gianluca5539 (9820 points)
3 6 44
answered Nov 26, 2021 by gianluca5539 (9,820 points)
try to open a new example.txt file without opening it with your OS, just open it with the code. Your text editor could have corrupted the file.
Quellocinese (2700 points)
2 4 22
answered Nov 26, 2021 by Quellocinese (2,700 points)
just comment or delete all the instructions given in the program01 file at the beginning