UnicodeEncodeError:“ ascii”编解码器无法编码字符 [英] UnicodeEncodeError: 'ascii' codec can't encode characters
问题描述
我有一个字典,其中包含网址响应。像这样:
I have a dict that's feed with url response. Like:
>>> d
{
0: {'data': u'<p>found "\u62c9\u67cf \u591a\u516c \u56ed"</p>'}
1: {'data': u'<p>some other data</p>'}
...
}
对此数据值使用 xml.etree.ElementTree
函数时( d [0 ] ['data']
)我得到最著名的错误消息:
While using xml.etree.ElementTree
function on this data values (d[0]['data']
) I get the most famous error message:
UnicodeEncodeError:'ascii'编解码器可以'不对字符进行编码...
我应该对该Unicode字符串进行什么处理才能使其适合ElementTree解析器?
What should I do to this Unicode string to make it suitable for ElementTree parser?
PS。请不要给我发送Unicode& Python解释。不幸的是,我已经阅读了所有内容,并且无法像希望的那样使用它。
PS. Please don't send me links with Unicode & Python explanation. I read it all already unfortunately, and can't make use of it, as hopefully others can.
推荐答案
将其手动编码为UTF-8:
You'll have to encode it manually, to UTF-8:
ElementTree.fromstring(d[0]['data'].encode('utf-8'))
,因为API仅将编码字节作为输入。 UTF-8是此类数据的理想默认值。
as the API only takes encoded bytes as input. UTF-8 is a good default for such data.
它可以从此处再次解码为unicode:
It'll be able to decode to unicode again from there:
>>> 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 "拉柏 多公 园"
这篇关于UnicodeEncodeError:“ ascii”编解码器无法编码字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!