简体   繁体   English

Python Django 编码错误,非 ASCII 字符 '\\xe5'

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

Hi, I ran into an encoding error with Python Django.嗨,我在使用 Python Django 时遇到了编码错误。 In my views.py, I have the following:在我的 views.py 中,我有以下内容:

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/ /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; 第 19 行文件 D:\\WinPython-32bit-2.7.5.3\\django_test\\article\\views.py 中的非 ASCII 字符 '\\xe5',但未声明编码; see http://www.python.org/peps/pep-0263.html for details (views.py, line 19) 有关详细信息,请参阅http://www.python.org/peps/pep-0263.html(views.py ,第 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正如 lalo 指出的,以下行必须在顶部

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

Thank you, all.谢谢你们。

Well, here you are:好吧,你来了:

Put # -*- coding: utf-8 -*- at top of file, it define de encoding.# -*- coding: utf-8 -*-放在文件顶部,它定义了编码。

The docs says:文档说:

Python will default to ASCII as standard encoding if no other encoding hints are given.如果没有给出其他编码提示,Python 将默认使用 ASCII 作为标准编码。

 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希望有帮助

If you read PEP 263 , it clearly says:如果您阅读PEP 263 ,它清楚地说明:

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 .对于3.32.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.出现在文件后面的“魔法注释”并不是魔法,它只是在不影响 Python 编译器的情况下误导读者的注释。

UTF-8 for u'哈哈' is '\\xe5\\x93\\x88\\xe5\\x93\\x88' , so those are the bytes in the file. u'哈哈' UTF-8 是'\\xe5\\x93\\x88\\xe5\\x93\\x88' ,所以这些是文件中的字节。 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);在最近的 Python 版本(包括 2.7 和所有 3.x)中,默认编码始终是 ASCII,除非文件以 UTF BOM 开头(就像某些 Microsoft 编辑器喜欢做的那样); even in 2.3-2.6 it's usually ASCII;即使在 2.3-2.6 中,它通常也是 ASCII; in earlier versions it's Latin-1.在早期版本中,它是 Latin-1。 Trying to interpret '\\xe5\\x93\\x88\\xe5\\x93\\x88' will fail with the exact exception you saw.尝试解释'\\xe5\\x93\\x88\\xe5\\x93\\x88'将失败,并显示您看到的确切异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM