Python 2.7 ElementTree.parse()中的Python UnicodeError

Python 2.7 ElementTree.parse()中的Python UnicodeError,python-2.7,unicode,encoding,python-requests,elementtree,Python 2.7,Unicode,Encoding,Python Requests,Elementtree,我正在编写一个API的python接口。接口必须使用Python2.7.x,不可能使用Python3.x 通过请求请求调用API。在我遇到问题的特定情况下,我收到一个错误: UnicodeEncodeError: 'ascii' codec can't encode character u'\xdf' in position 24037: ordinal not in range(128) 我已经尝试了几种逐渐不那么幼稚的方法来处理这个问题。都失败了 一, tree=ElementTree()

我正在编写一个API的python接口。接口必须使用Python2.7.x,不可能使用Python3.x

通过
请求
请求调用API。在我遇到问题的特定情况下,我收到一个错误:

UnicodeEncodeError: 'ascii' codec can't encode character u'\xdf' in position 24037: ordinal not in range(128)
我已经尝试了几种逐渐不那么幼稚的方法来处理这个问题。都失败了

一,

tree=ElementTree().parse(StringIO(apireult.body))

二,

三,

所有操作均失败,并显示上述错误消息。在所有情况下,
apireult.body
都是字符串。我的问题是,当我显式使用非ascii编码的解析器时,为什么在案例2和案例3中调用ascii编解码器


当然,我该如何解决这个问题呢?

这是一个python API。问题是服务器返回unicode,因为API没有放入
内容类型:text/html;标题中的字符集=utf-8
。我可以在收到尸体后用UTF-8编码,然后它就可以工作了。或者我可以修复API来解决此问题。

我们如何重现此问题?如果可能,请分享1。您如何在
请求
级别2上接收
apireult.body
数据。
apireult.body
本身的性质/内容
parser = XMLParser(encoding="UTF-8")
tree = ElementTree().parse( StringIO(apiResult.body), parser=parser )
parser = XMLParser(encoding="ISO-8859-1")
tree = ElementTree().parse( StringIO(apiResult.body), parser=parser )