无法在Google App Engine中使用Python 2.5在web端正确显示拉丁口音

无法在Google App Engine中使用Python 2.5在web端正确显示拉丁口音,python,google-app-engine,utf-8,Python,Google App Engine,Utf 8,非常基本的Python关注点>我无法在我的main.py API中的自定义TinyWebDB数据库文件(以Python 2.5编码)的web端显示正确的拉丁口音: 在web上:tag=滚珠轴承/value=roulement\u00e0钢坯=>NOK 在安卓手机上:tag=ball-bearing/value=roulementábilles=>OK 注意:另外,如果在main.py文件中引入任何拉丁字符,则在运行它时会收到错误消息 解决办法是什么 从我的代码中摘录: #!/usr/bin/en

非常基本的Python关注点>我无法在我的main.py API中的自定义TinyWebDB数据库文件(以Python 2.5编码)的web端显示正确的拉丁口音:

在web上:tag=滚珠轴承/value=roulement\u00e0钢坯=>NOK

在安卓手机上:tag=ball-bearing/value=roulementábilles=>OK

注意:另外,如果在main.py文件中引入任何拉丁字符,则在运行它时会收到错误消息

解决办法是什么

从我的代码中摘录:

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

import codecs

class StoredData(db.Model):
    tag = db.StringProperty()
    value = db.StringProperty(multiline=True)
    date = db.DateTimeProperty(required=True, auto_now=True)


def get_value(self, tag):
    entry = db.GqlQuery("SELECT * FROM StoredData where tag = :1", tag).get()
        if entry:
            value = entry.value
        else:
            value = "No result"
        WritePhoneOrWeb(self, lambda : json.dump(value, self.response.out))

#### Write response to the phone or to the Web depending on fmt
#### Handler is an appengine request handler.  writer is a thunk
#### (i.e. a procedure of no arguments) that does the write when invoked.
def WritePhoneOrWeb(handler, writer):
    if handler.request.get('fmt') == "html":
        WritePhoneOrWebToWeb(handler, writer)
    else:
        handler.response.headers['Content-Type'] = 'application/jsonrequest'
    writer()

#### Write to the Web (without checking fmt)
def WriteToWeb(handler, writer):
    handler.response.headers['Content-Type'] = 'text/html;charset=UTF-8'
    handler.response.out.write('<html><body>')
    writer()
    WriteWebFooter(handler, writer)
我在代码中添加了:

 value = unicode(value)
 value = value.encode('ascii','xmlcharrefreplace')
而且它工作得很好

菲利普