相关疑难解决方法(0)

如何使用ElementTree正确解析utf-8 xml?

我需要帮助才能理解为什么用xml.etree.ElementTree解析我的xml文件*会产生以下错误.

*我的测试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)
Run Code Online (Sandbox Code Playgroud)

结果1:

UnicodeEncodeError: 'ascii' codec can't encode characters in position 236-238: ordinal not in range(128)
Run Code Online (Sandbox Code Playgroud)

我的第二次尝试:

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)
Run Code Online (Sandbox Code Playgroud)

结果2:

AttributeError: 'file' object has no attribute 'getiterator'
Run Code Online (Sandbox Code Playgroud)

请解释上述错误并评论可能的解决方案.

python xml elementtree xml-parsing python-2.7

14
推荐指数
1
解决办法
3万
查看次数

标签 统计

elementtree ×1

python ×1

python-2.7 ×1

xml ×1

xml-parsing ×1