“ascii”编解码器无法解码字节(使用 django 时出现问题)
我在 python 中编写了一个简单的 html 解析类,它似乎工作正常,然后我尝试将它与 django 一起使用,但出现此错误:
'ascii' codec can't decode byte 0xc2 in position 54465: ordinal not in range(128)
这很奇怪,因为我添加了以下内容:#encoding: utf-8
在班上名列前茅。我对编码不太了解,但有人可以告诉我这里发生了什么吗?顺便说一句,我还确保源 html 已经是 utf-8 格式。谢谢!
I wrote a simple html parsing class in python and it seems to work fine and then I try to use it with django and I get this error:
'ascii' codec can't decode byte 0xc2 in position 54465: ordinal not in range(128)
which is strange because I added this: # encoding: utf-8
to the top of my class. I don't really know much about encoding but can someone perhaps give me an idea of what's going here? Btw, I also insured that the source html was already in utf-8. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试将该行放在文件的顶部。根据 PEP 263,它必须位于最上面两行。
Try putting that line at the top of your file. According to PEP 263, it has to be in the top two lines.
好的,我明白了。我需要做的就是在 django 视图中包含
# -*-coding: utf-8 -*-
就解决了这个问题!okay, I got it. All I needed to do was include
# -*- coding: utf-8 -*-
in the django view as well and that solved it!