UnicodeEncodeError:'ascii'编解码器无法编码字符

the*_*eta 11 python unicode elementtree

我有一个dict,它是url响应的feed.喜欢:

>>> d
{
0: {'data': u'<p>found "\u62c9\u67cf \u591a\u516c \u56ed"</p>'}
1: {'data': u'<p>some other data</p>'}
...
}
Run Code Online (Sandbox Code Playgroud)

xml.etree.ElementTree在这个数据值(d[0]['data'])上使用函数时,我得到了最着名的错误消息:

UnicodeEncodeError: 'ascii' codec can't encode characters...

我应该怎么做这个Unicode字符串,使其适合ElementTree解析器?

PS.请不要向我发送带有Unicode和Python解释的链接.我已经很遗憾地阅读了这一切,并且无法利用它,希望其他人可以.

Mar*_*ers 25

你必须手动编码为UTF-8:

ElementTree.fromstring(d[0]['data'].encode('utf-8'))
Run Code Online (Sandbox Code Playgroud)

因为API仅将编码字节作为输入.UTF-8是此类数据的良好默认值.

它将能够从那里再次解码为unicode:

>>> from xml.etree import ElementTree
>>> p = ElementTree.fromstring(u'<p>found "\u62c9\u67cf \u591a\u516c \u56ed"</p>'.encode('utf8'))
>>> p.text
u'found "\u62c9\u67cf \u591a\u516c \u56ed"'
>>> print p.text
found "?? ?? ?"
Run Code Online (Sandbox Code Playgroud)