没有root的Python XML解析 [英] Python XML Parsing without root

查看:31
本文介绍了没有root的Python XML解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想解析一个相当大的类似xml的文件,其中没有任何根元素.该文件的格式为:

I wanted to parse a fairly huge xml-like file which doesn't have any root element. The format of the file is:

<tag1>
<tag2>
</tag2>
</tag1>

<tag1>
<tag3/>
</tag1>

我尝试使用Element-Tree,但返回了无根"错误.还有其他可用于解析此文件的python库吗?提前致谢!:)

I tried using Element-Tree but it returned a "no root" error. Is there any other python library which can be used for parsing this file? Thanks in advance! :)

PS:我尝试添加一个额外的标签来包装整个文件,然后使用Element-Tree对其进行解析.但是,我想使用一些更有效的方法,无需更改原始xml文件.

PS: I tried adding an extra tag to wrap the entire file and then parse it using Element-Tree. However, I would like to use some more efficient method, in which I would not need to alter the original xml file.

推荐答案

ElementTree.fromstringlist accepts an iterable (that yields strings).

itertools.chain :

import itertools
import xml.etree.ElementTree as ET
# import xml.etree.cElementTree as ET

with open('xml-like-file.xml') as f:
    it = itertools.chain('<root>', f, '</root>')
    root = ET.fromstringlist(it)

# Do something with `root`
root.find('.//tag3')

这篇关于没有root的Python XML解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆