我需要帮助才能理解为什么要解析我的xml文件* xml.etree.ElementTree 产生以下错误。
*我的测试xml文件包含阿拉伯字符。
任务:
打开并解析 utf8_file.xml
文件。
我的第一次尝试:
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)
结果1:
UnicodeEncodeError: 'ascii' codec can't encode characters in position 236-238: ordinal not in range(128)
我的第二次尝试:
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)
结果2:
AttributeError: 'file' object has no attribute 'getiterator'
请解释上面的错误并评论可能的解决方案。