Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UnicodeDecodeError, Invalid continuation byte again

I can't figure out how to solve these problems once for all. I first encountered these problems when I tried to write "è" (i'm Italian). After some research, I found out that adding "#coding: utf-8" at the very beginning seemed to solve the problem....UNTIL NOW.

I edited a code wrote 1 or 2 days ago..it worked perfectly.... now, whenever i run the script, it doesn't work: it never starts, and I'm stuck with this error:

SyntaxError: 'utf-8' codec can't decode byte 0xe0 in position 32: invalid continuation byte.

The problem is... position 32? Where? what's the problematic line? I don't know exactly what I added, because I made a couple of changes. Trying to execute in debug mode doesn't help either, when I "Step Into" at the very beginning of the script, the error shows up immediately (by the way, i'm using Wingware 101 as an IDLE,I'm on Win7). Sorry if I don't have enough information, I could post the code but I'm afraid to do so, it's a mess written in Italian, maybe it could be not easy to understand exactly what's going on.

Thank you for replies and happy holidays!

like image 264
Russell Teapot Avatar asked Apr 27 '26 07:04

Russell Teapot


1 Answers

#coding: utf8 is a declaration that the source code is saved in UTF-8. Make sure that is actually the encoding of the source file. For example, the following file was created in Windows Notepad and saved as "ANSI", which on US Windows is the Windows-1252 encoding:

#coding: utf8
print('hàllo')

It produces the following error on Python 2.7:

  File "test.py", line 2
SyntaxError: 'utf8' codec can't decode byte 0xe0 in position 8: invalid continuation byte

As you can see, then 8th position (counting from 0) of line 2 is à, which in Windows-1252 is byte 0xe0. The wrong encoding is used and the error message is clear.

Either declare the correct encoding for your source file, or re-save the source file in UTF-8.

Note: I don't have Python 3.4 installed, but Python 3.5 gives a less clear error message:

  File "x.py", line 1
SyntaxError: encoding problem: utf8

It doesn't match your error message, though, but still indicates the file is not declared with the right encoding.

like image 53
Mark Tolonen Avatar answered Apr 29 '26 20:04

Mark Tolonen