I need help to understand why parsing my xml file* with xml.etree.ElementTree produces the following errors.
*My test xml file contains arabic characters.
Task:
Open and parse utf8_file.xml
file.
My first try:
import xml.etree.ElementTree as etree
with codecs.open('utf8_file.xml', 'r', encoding='utf-8') as utf8_file:
xml_tree = etree.parse(utf8_file)
Result 1:
UnicodeEncodeError: 'ascii' codec can't encode characters in position 236-238: ordinal not in range(128)
My second try:
import xml.etree.ElementTree as etree
with codecs.open('utf8_file.xml', 'r', encoding='utf-8') as utf8_file:
xml_string = etree.tostring(utf8_file, encoding='utf-8', method='xml')
xml_tree = etree.fromstring(xml_string)
Result 2:
AttributeError: 'file' object has no attribute 'getiterator'
Please explain the errors above and comment on the possible solution.