Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Django Encoding Error, Non-ASCII character '\xe5'

Hi, I ran into an encoding error with Python Django. In my views.py, I have the following:

from django.shortcuts import render
from django.http import HttpResponse
from django.template.loader import get_template
from django.template import Context
# Create your views here.

def hello(request):
    name = 'Mike'
    html = '<html><body>Hi %s, this seems to have !!!!worked!</body></html>' % name
    return HttpResponse(html)

def hello2(request):
    name = 'Andrew'
    html = '<html><body>Hi %s, this seems to have !!!!worked!</body></html>' % name
    return HttpResponse(html)

# -*- coding: utf-8 -*-
def hello3_template(request):
    name = u'哈哈'
    t = get_template('hello3.html')
    html = t.render(Context({'name' : name}))
    return HttpResponse(html)

I got the following error:

SyntaxError at /hello3_template/

Non-ASCII character '\xe5' in file D:\WinPython-32bit-2.7.5.3\django_test\article\views.py on line 19, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details (views.py, line 19)

I look up that link, but I am still puzzled on how to resolve it.

Could you help? Thanks, smallbee

As lalo points out, the following line has to be on the top

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

Thank you, all.

like image 905
smallbee Avatar asked Dec 21 '13 01:12

smallbee


2 Answers

Well, here you are:

Put # -*- coding: utf-8 -*- at top of file, it define de encoding.

The docs says:

Python will default to ASCII as standard encoding if no other encoding hints are given.

To define a source code encoding, a magic comment must
be placed into the source files either as first or second
line in the file, such as:

So, you code must begin:

# -*- coding: utf-8 -*-
from django.shortcuts import render
from django.http import HttpResponse
from django.template.loader import get_template
...

Hope helps

like image 82
Leandro Avatar answered Oct 23 '22 16:10

Leandro


If you read PEP 263, it clearly says:

To define a source code encoding, a magic comment must be placed into the source files either as first or second line in the file…

(The original proposal said that it had to be the first line after the #!, if any, but presumably it turned out to be easier to implement with the "first or second line" rule.)

The actual reference docs describe the same thing, in a less friendly but more rigorous way, for 3.3 and 2.7.

A "magic comment" that appears later in the file is not magic, it's just a comment to mislead your readers without affecting the Python compiler.

UTF-8 for u'哈哈' is '\xe5\x93\x88\xe5\x93\x88', so those are the bytes in the file. In recent Python versions (including 2.7 and all 3.x), the default encoding is always ASCII unless the file starts with a UTF BOM (as some Microsoft editors like to do); even in 2.3-2.6 it's usually ASCII; in earlier versions it's Latin-1. Trying to interpret '\xe5\x93\x88\xe5\x93\x88' will fail with the exact exception you saw.

like image 30
abarnert Avatar answered Oct 23 '22 15:10

abarnert