Weird Unicode error

Nilats (3710 points)
6 9 27
asked Nov 25, 2021 in HW6 by Nilats (3,710 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.

128 views

5 Answers

Best answer
GabrielAlexandru (7360 points)
2 4 28
answered Nov 26, 2021 by GabrielAlexandru (7,360 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 .ita.py file. When they switched to the .eng.py everything worked fine.

I don’t know if this applies to you too, but you can try!
andrea.sterbini (167380 points)
487 897 1724
commented Nov 26, 2021 by andrea.sterbini (167,380 points)

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

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

Nilats (3710 points)
6 9 27
commented Nov 26, 2021 by Nilats (3,710 points)
Yeah, I just copy-pasted the whole code to the English file and now it's working.
n
nane.harutyunyan (750 points)
0 0 4
answered Nov 25, 2021 by nane.harutyunyan (750 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 (3710 points)
6 9 27
commented Nov 25, 2021 by Nilats (3,710 points)
I've already done it.
I also tried encoding="latin-1"
rokshana03 (7100 points)
1 2 25
answered Nov 26, 2021 by rokshana03 (7,100 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 (3710 points)
6 9 27
commented Nov 26, 2021 by Nilats (3,710 points)
I already tried those as well and nope, it's not working.
gianluca5539 (9710 points)
2 4 42
answered Nov 26, 2021 by gianluca5539 (9,710 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 (2670 points)
2 3 22
answered Nov 26, 2021 by Quellocinese (2,670 points)
just comment or delete all the instructions given in the program01 file at the beginning