我有一个包含URL响应的字典。例如:
>>> 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']
)时,我遇到了最常见的错误信息:UnicodeEncodeError: 'ascii' codec can't encode characters...
。为了使这个 Unicode 字符串适合 ElementTree 解析器,我该怎么做呢?
PS. 请不要发送有关 Unicode 和 Python 解释的链接。不幸的是,我已经阅读了所有内容,但无法利用它,希望其他人可以。
ElementTree.tostring
上。请尝试使用ElementTree.tostring(p, method='text')
并告诉我为什么它不起作用?谢谢。 - theta.tostring()
有一个可选参数'encoding',默认情况下可能设置为ascii,因此添加encoding ='utf-8'
即可解决问题。干杯 - theta