如何在 Bottle 框架中渲染阿拉伯字符串?
我正在学习 Bottle 框架,并且是 Python 的新手。刚刚偶然发现了这个困难。当我编写一个简单的方法来返回阿拉伯字符串时,例如:
@route('/hello')
def hello():
return u'سلام'
我在终端中收到此错误消息:
语法错误:文件 hello.py 第 15 行中存在非 ASCII 字符“\xd8”, 但没有声明编码;请参阅http://www.python.org/peps/pep-0263.html 了解详情
我已从 Bottle 导入所有内容,并尝试添加 docs 中提到的其他方法其中谈到“更改默认编码”,但我无法解决该问题。所以我很感激你的提示。
I am learning Bottle framework and new to Python. Just stumbled upon this difficulty. When I write a simple method to return a an Arabic string like:
@route('/hello')
def hello():
return u'سلام'
I get this error message in the terminal:
SyntaxError: Non-ASCII character '\xd8' in file hello.py on line 15,
but no encoding declared; see http://www.python.org/peps/pep-0263.html
for details
I have imported all from bottle and tried adding other methods mentioned in the docs where it talks about "Changing the Default Encoding" however I was unable to resolve the issue. So I appreciate your hints.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
只需添加
到文件顶部
just add
on the top of your file
将文件另存为 utf-8 并插入
为文件的第一行
Save your file as utf-8 and insert
as the first line of your file
在脚本顶部输入以下内容:
问题是,您的脚本可能使用 latin1 编码 (ISO 8859-1) 运行,与 UTF-8 相比,该编码受到限制
At the top of your script, enter this:
The thing is, your script might run with the latin1 encoding (ISO 8859-1), which is limited compared to UTF-8
这是我的测试代码:
在我的编辑器中,我选择“文件”>“另存为...,然后选择 Unicode (UTF-8) 作为文本编码,并另存为 hello.py
然后下载 从github上下载最新版本的bottle.py,并把它和hello.py放在同一个文件夹中(例如bottle-test)
,运行一下,看起来完全没有问题。
Here is my code for testing:
In my editor, I choose File > Save As..., then select Unicode (UTF-8) as Text Encoding, and saved as hello.py
Then download the lastest version of bottle.py from github, and put it in the same folder(e.g. bottle-test) with hello.py
Run it, and seems no problems at all.