非常基本Unicode Python关注>我无法在我的CSV main.py
API中的Python 2.5中的自定义TinyWebDB数据库(UTF-8中编码的Google App Engine文件)中显示正确的拉丁口音:
在网上:tag = ball bearing / value = roulement \ u00e0 billes => NOK!
在Android手机上: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)
答案 0 :(得分:0)
我添加到我的代码中:
value = unicode(value)
value = value.encode('ascii','xmlcharrefreplace')
它完美无缺。
菲利普