0

非常基本的Unicode Python 问题> 我无法在我的Google App Engine API中的 Python 2.5 中的自定义 TinyWebDB 数据库(以UTF-8编码的CSV文件)中在 Web 端显示正确的拉丁语重音: main.py

在网络上:标签 = 滚珠轴承 / 价值 = roulement \u00e0 billes => NOK!

在 Android 手机上:标签 = 滚珠轴承 / 价值 = 规则 à billes => 好!

注意:另外,如果我在文件中引入任何拉丁字符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)
4

1 回答 1

0

我添加到我的代码中:

 value = unicode(value)
 value = value.encode('ascii','xmlcharrefreplace')

它工作得很好。

菲利普

于 2012-06-27T20:22:44.277 回答