UnicodeEncodeError:“ascii”编解码器无法编码字符

2024-09-27 07:28:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个带有url响应的提要dict。比如:

>>> d
{
0: {'data': u'<p>found "\u62c9\u67cf \u591a\u516c \u56ed"</p>'}
1: {'data': u'<p>some other data</p>'}
...
}

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

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

为了使这个Unicode字符串适合ElementTree解析器,我应该对它做些什么?

另外,请不要给我发送带有Unicode&Python解释的链接。我已经很不幸地读过了,不能像其他人那样利用它。


Tags: 数据urldataunicodesomexmldictother
1条回答
网友
1楼 · 发布于 2024-09-27 07:28:51

您必须手动将其编码为UTF-8:

ElementTree.fromstring(d[0]['data'].encode('utf-8'))

因为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 "拉柏 多公 园"

相关问题 更多 >

    热门问题