Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python xml.etree.ElementTree和unicode findtext_Python_Xml_Unicode_Elementtree - Fatal编程技术网

Python xml.etree.ElementTree和unicode findtext

Python xml.etree.ElementTree和unicode findtext,python,xml,unicode,elementtree,Python,Xml,Unicode,Elementtree,我正在尝试使用xml.etree.ElementTree模块中的iterparse解析Medline xml文档。除了一些文本包含非ascii字符外,所有这些都运行良好。我没有看到使用findtext处理unicode的方法。有什么建议吗?您是否尝试过使用utf8编码flah打开文件: fd = open('some.xml', mode='r', encoding='utf-8') xml.etree.ElementTree.iterparse(fd) 或使用解码: fd = open('s

我正在尝试使用xml.etree.ElementTree模块中的iterparse解析Medline xml文档。除了一些文本包含非ascii字符外,所有这些都运行良好。我没有看到使用findtext处理unicode的方法。有什么建议吗?

您是否尝试过使用utf8编码flah打开文件:

fd = open('some.xml', mode='r', encoding='utf-8')
xml.etree.ElementTree.iterparse(fd)
或使用解码:

fd = open('some.xml', mode='r')
sio = StringIO(fd.read().decode("utf-8"))
xml.etree.ElementTree.iterparse(sio)

除了上面的答案之外,这是一篇非常有用的帖子


我认为这应该行得通,但我还是会出错。下一步是验证编码是否确实是UTF-8