我有一个10gbxml文件,其中包含不同块的列表。以下是我的文件片段:
<image>
<ref>www.test.com</ref>
<label/>
<number>0</number>
<ID>ID0</ID>
<name>test1</name>
<comment>
<line number="0">This is a comment</line>
<line number="1">This is also another comment</line>
</comment>
<creationDate>2017-02-13T15:46:16-04:00</creationDate>
</image>
<result>
<ref>www.test1.com</ref>
<label/>
<number>001</number>
<ID>RE1</ID>
<name>test2</name>
<comment>
<line number="0">This is a comment2</line>
</comment>
<creationDate>2017-01-13T15:46:16-04:00</creationDate>
</result>
<image>
<ref>www.test3.com</ref>
<label/>
<number>1</number>
<ID>ID1</ID>
<value>10030</value>
<name>test3</name>
<comment>
<line number="0">This is a comment3</line>
</comment>
<creationDate>2017-04-13T15:46:16-04:00</creationDate>
</image>
因此,我的目标是使用celementree的iterparse以序列化模式解析我的文件,但希望一次获取每个块。例如,我喜欢获取image的整个块,然后解析该块中的值。
例如,我需要得到第一个图像块(*<image>... </image>*
)块,然后打印其中的值,这些值是www.test.com网站,0,id0,test1,这是一条评论和2017-02-13T15:46:16-04:00。
因此,我使用了以下代码,但似乎它只能逐行读取xml文件,也无法打印每行或元素内的值:
^{pr2}$你能帮我解决这个问题吗。我对xml解析完全陌生。 我还想将每个解析的块转换成python中的字典。有可能吗?在
它不是“逐行”读取XML文件。它在每个元素的末尾返回一个
end
事件。也就是说,如果您的输入文件如下所示:对
^{pr2}$iterparse
的简单调用的返回值序列为:如果需要,还可以在每个元素的开始处receive ^{} events ,如下所示:
其输出为:
如果我想为每个
location
构建一个widgets
的列表,那么我可能希望通过初始化一个列表来响应start
事件,然后将每个新的小部件附加到该列表中,直到到达end元素,如中所示:其输出为:
我希望这能让您了解如何处理输入文件中的每个感兴趣的块。在
相关问题 更多 >
编程相关推荐