Python 两个xml文件的逐行比较

Python 两个xml文件的逐行比较,python,xml,compare,Python,Xml,Compare,我想比较两个格式相似的xml文件 示例f1.xml: <apple name="a" mb="15" lb="0" write="true" value="1"/> <apple name="b" mb="31" lb="16" write="true" value="2"/> <apple name="c" mb="32" lb="32" write="true" value="3"/> <apple name="a" mb="15" lb="0"

我想比较两个格式相似的xml文件

示例
f1.xml

<apple name="a" mb="15" lb="0" write="true" value="1"/>
<apple name="b" mb="31" lb="16" write="true" value="2"/>
<apple name="c" mb="32" lb="32" write="true" value="3"/>
<apple name="a" mb="15" lb="0" write="true" value="1"/>
<apple name="b" mb="31" lb="16" write="true" value="3"/>
<apple name="c" mb="32" lb="32" write="true" value="2"/>
如果同一个苹果名称的值不同,我想逐行比较并打印出来


如何使用Python实现这一点

首先,您需要通过给xml一个根元素使其成为有效的xml

然后使用lxml解析它

然后用你喜欢的函数比较它们

这个例子并不是最简单的方法,但它确实以一种展示了许多可以使用的不同功能的方式对它进行了分解

from lxml import etree

def dem_apples(xml1,xml2,join_on='name'):

    tree1 = etree.fromstring(xml1)
    tree2 = etree.fromstring(xml2)

    for a1 in tree1.xpath('./apple'):
        a1_attr_set = set(dict(a1.attrib).items())
        a2_list = tree2.xpath('./apple[@{0}="{1}"]'.\
         format(join_on,a1.get(join_on)))
        for a2 in a2_list:
            a2_attr_set =  set(dict(a2.attrib).items())
            diff = a1_attr_set - a2_attr_set
            if diff:

                print(a1.get(join_on),diff,a2_attr_set-a1_attr_set)

if __name__ == '__main__':

    xml_string1="""
    <fruit>
    <apple name="a" mb="15" lb="0" write="true" value="1"/>
    <apple name="b" mb="31" lb="16" write="true" value="2"/>
    <apple name="c" mb="32" lb="32" write="true" value="3"/>
    </fruit>
    """

    xml_string2="""
    <fruit>
    <apple name="a" mb="15" lb="0" write="true" value="1"/>
    <apple name="b" mb="31" lb="16" write="true" value="3"/>
    <apple name="c" mb="32" lb="32" write="true" value="2"/>
    </fruit>
    """
    dem_apples(xml_string1,xml_string2)
从lxml导入etree
def dem_apples(xml1,xml2,join_on='name'):
tree1=etree.fromstring(xml1)
tree2=etree.fromstring(xml2)
对于tree1.xpath('./apple')中的a1:
a1_attr_set=set(dict(a1.attrib.items())
a2_list=tree2.xpath('./apple[@{0}=“{1}]”\
格式(join_on,a1.get(join_on)))
对于a2_列表中的a2:
a2_attr_set=set(dict(a2.attrib.items())
差异=a1_属性集-a2_属性集
如果有差异:
打印(a1.get(连接),差异,a2属性集-a1属性集)
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
xml_string1=“”
"""
xml_string2=“”
"""
dem_apples(xml_string1,xml_string2)