I have a function that should save the xml from response to file. Input arguments are response and name of file (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))
That returns me an error:
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)
An error with using 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)
I have tried encoding utf 8, did not help either.
Can anybody halp me eliminate this error?
142489-142490? Theoretically you could do a slice likeresponse.text[142489:142490+1]type(response.text)yieldsbytes?