6

When I launch my app, I get this error UnicodeDecodeError: 'utf8' codec can't decode byte 0xe9 in position 2566: invalid continuation byte. I use UTF8 in my HTML file

<meta charset="utf-8" />

and in my Python file

# -*- coding: utf-8 -*-
self.response.headers['Content-Type'] = 'text/html; charset=UTF-8'

I saw some solutions on the Web using the encode() function but I don't want to insert text in the Python file but in HTML file.

tsil
  • 2,069
  • 7
  • 29
  • 43
  • 1
    Can you post a traceback? Where is that byte coming from? Are you actually saving the file as UTF-8, or just telling Python that you did? – Wooble May 31 '12 at 17:04
  • possible duplicate of [UnicodeDecodeError, invalid continuation byte](http://stackoverflow.com/questions/5552555/unicodedecodeerror-invalid-continuation-byte) – Wooble May 31 '12 at 17:08
  • It appears the editor you're using does not save as UTF-8 by default. – Mark Ransom May 31 '12 at 17:42
  • @MarkRansom I use IDLE and I configure it to save files in UTF-8 by default. – tsil May 31 '12 at 18:23
  • 1
    @IsmaelToé, does that include your HTML too? – Mark Ransom May 31 '12 at 18:57
  • Ouf! Thank you @MarkRansom I just checked and my HTML files were in ANSI (I use Notepad++). Encode in UTF-8 and it works. – tsil May 31 '12 at 23:02

2 Answers2

9

If you are using Notepad++ make sure the "encoding" (in the menu) of all your files is set to "UTF-8".

I don't know for other editors but that might be the problem.

Panda_Cat
  • 106
  • 2
  • Oh gosh! I just rendered a built-in form in Django and it told me UnicodeEncodeError, I did not understand why since it was a native form and then, thanks to your answer, I checked my N++ encoding :) You saved my day :) Thank you Panda_cat – Nicolas W. Feb 28 '13 at 08:19
-4

change to

# -*- coding: latin1 -*-

0xe9 is part of the latin1 charset.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Joran Beasley
  • 110,522
  • 12
  • 160
  • 179