开发者

How to render Arabic strings in Bottle framework?

I am learning Bo开发者_C百科ttle 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.


Here is my code for testing:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from bottle import *

@route('/hello')
def hello():
    return u'سلام'

run(host='127.0.0.1', port=8080,reloader=True)

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.

~$ python --version
Python 2.6.7
~$ cd bottle-test
bottle-test$ python hello.py 

How to render Arabic strings in Bottle framework?


just add

# -*- coding: whatever-encoding-you-use -*-

on the top of your file


Save your file as utf-8 and insert

#encoding: utf-8

as the first line of your file


At the top of your script, enter this:

# encoding: utf-8

The thing is, your script might run with the latin1 encoding (ISO 8859-1), which is limited compared to UTF-8

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜