python其余保存解析的xml文档-错误编码

时间:2019-04-09 11:06:17

标签: python xml elementtree

我有一个应将XML从响应保存到文件的功能。输入参数是响应和文件名(objNm:)

def getXml ( response, objNm):
    root = ET.fromstring(response.text)
    tree = ET.ElementTree(root)
    xmlNm = objNm + ".xml"
    tree.write(open(xmlNm, 'w'), encoding='unicode')
    print('Object {} was succsessfully created.'.format(xmlNm))

那给我返回一个错误:

Traceback (most recent call last): File "test.py", line 56, 
    in <module> getXml(response, 'test_example') 
    File "test.py", line 17, in getXml root = ET.fromstring(response.text) 
    File "/usr/lib64/python2.7/xml/etree/ElementTree.py", line 1300, in XML parser.feed(text) 
    File "/usr/lib64/python2.7/xml/etree/ElementTree.py", line 1640, in feed self._parser.Parse(data, 0) 
    UnicodeEncodeError: 'ascii' codec can't encode characters in position 142489-142490: ordinal not in range(128)

使用root = ET.fromstring(response.text.decode('utf-8'))的错误

Traceback (most recent call last):
  File "test.py", line 56, in <module>
    getXml(response, 'test_example')
  File "test.py", line 17, in getXml
    root = ET.fromstring(response.text.decode('utf-8'))
  File "/usr/lib64/python2.7/encodings/utf_8.py", line 16, in decode
     return codecs.utf_8_decode(input, errors, True)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 142489-142490: ordinal not in range(128)

我尝试编码utf 8,也没有帮助。

有人能阻止我消除此错误吗?

1 个答案:

答案 0 :(得分:1)

如果您使用的是python2.7,则通常默认情况下以ascii模式打开python文件。您需要在文件顶部指定# -*- coding: utf-8 -*-

可以完成的其他一些事情:

调用encoded_text = response.text.encode('utf-8', 'replace'),然后将其用于fromstring(encoded_text)

通过以下方式进行测试:

import codecs
data = u'abcdëëaaë'
data = data.encode('utf-8', 'replace')
something = codecs.utf_8_decode(data, 'strict', True)
print something

另一种方法是将utf-8系统范围设置为:

import sys
reload(sys)
sys.setdefaultencoding('utf-8')