【发布时间】:2015-06-29 21:36:43
【问题描述】:
我目前正在处理一个 python 项目,但遇到了一个与使用 python 比较两个 XML 文件相关的小问题。现在假设我们有两个 xml 文件:
一个文件:
<m1:time timeinterval="5">
<m1:vehicle distance="40" speed="5"\>
<m1:location hours = "1" path = '1'\>
<m1:feature color="2" type="a">564</m1:feature>
<m1:feature color="3" type="b">570</m1:feature>
<m1:feature color="4" type="c">570</m1:feature>
<\m1:location>
<m1:location hours = "5" path = '1'\>
<m1:feature color="6" type="a">560</m1:feature>
<m1:feature color="7" type="b">570</m1:feature>
<m1:feature color="8" type="c">580</m1:feature>
<\m1:location>
<m1:location hours = "9" path = '1'\>
<m1:feature color="10" type="a">560</m1:feature>
<m1:feature color="11" type="b">570</m1:feature>
<m1:feature color="12" type="c">580</m1:feature>
<\m1:location>
</m1:time>
B文件:
<m1:time timeinterval="6">
<m1:vehicle distance="40" speed="5"\>
<m1:location hours = "5" path = '1'\>
<m1:feature color="6" type="a">560</m1:feature>
<m1:feature color="7" type="b">570</m1:feature>
<m1:feature color="8" type="c">580</m1:feature>
<\m1:location>
<m1:location hours = "1" path = '1'\>
<m1:feature color="2" type="a">564</m1:feature>
<m1:feature color="3" type="b">570</m1:feature>
<m1:feature color="4" type="c">570</m1:feature>
<\m1:location>
<m1:location hours = "9" path = '1'\>
<m1:feature color="10" type="a">560</m1:feature>
<m1:feature color="11" type="b">570</m1:feature>
<m1:feature color="12" type="c">580</m1:feature>
<\m1:location>
</m1:time>
- 我想问的是如何比较A文件和B文件 确保虽然“位置”元素的顺序不同 在这两个文件中,它们仍然使用 python 显示相同吗?
- 我尝试过各种方法,也尝试过参考 this 问题,但在这个项目中,我想开发一个 我自己的方法,我不能使用任何已经可用的工具。
到目前为止我尝试过的方法是:
我正在使用 LXML,我从 A 文件中获取子项的各个属性并将它们存储在列表中。然后我将 B 文件的元素和子属性与存储在该列表中的值进行比较。
首先,这种方法不起作用,我也想不出任何有效的程序来完成这项任务。你们能对此有所了解吗?
谢谢。
【问题讨论】:
标签: python xml comparison lxml