插入UTF-8字符(中国,阿拉伯语,日语..等)记录到GAE数据存储编程与Python

Iva*_*ter 2 python google-app-engine character-encoding google-cloud-datastore

我只想构建GAE内置的简单UI翻译(使用python SDK).

def add_translation(self, pid=None):
    trans = Translation()
    trans.tlang = db.Key("agtwaW1kZXNpZ25lcnITCxILQXBwTGFuZ3VhZ2UY8aIEDA")
    trans.ttype = "UI"
    trans.transid = "ui-about"
    trans.content = "????"
    trans.put()
Run Code Online (Sandbox Code Playgroud)

这导致编码错误:

UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position 0: ordinal not in range(128)
Run Code Online (Sandbox Code Playgroud)

如何使用unicode(utf-8)字符编码正确的插入内容?

Ada*_*tan 6

使用u符号:

>>> s=u"????"
>>> print s
????
Run Code Online (Sandbox Code Playgroud)

或明确说明编码:

>>> s=unicode('??? ???', 'utf8')
>>> print s
??? ??? 
Run Code Online (Sandbox Code Playgroud)

阅读python文档站点中的Unicode HOWTO页面.