Comparing two xml files in python

后端 未结 3 1418
别那么骄傲
别那么骄傲 2020-12-03 12:28

I am new to programming in python,´and i have some troubles understanding the concept. I wish to compare two xml files. These xml files are quite large. I will give an examp

3条回答
  •  一个人的身影
    2020-12-03 12:50

    Another script using xml.etree. Its awful but it works :)

    #!/usr/bin/env python
    
    import sys
    import xml.etree.ElementTree as ET
    
    from termcolor import colored
    
    tree1 = ET.parse(sys.argv[1])
    root1 = tree1.getroot()
    
    tree2 = ET.parse(sys.argv[2])
    root2 = tree2.getroot()
    
    class Element:
        def __init__(self,e):
            self.name = e.tag
            self.subs = {}
            self.atts = {}
            for child in e:
                self.subs[child.tag] = Element(child)
    
            for att in e.attrib.keys():
                self.atts[att] = e.attrib[att]
    
            print "name: %s, len(subs) = %d, len(atts) = %d" % ( self.name, len(self.subs), len(self.atts) )
    
        def compare(self,el):
            if self.name!=el.name:
                raise RuntimeError("Two names are not the same")
            print "----------------------------------------------------------------"
            print self.name
            print "----------------------------------------------------------------"
            for att in self.atts.keys():
                v1 = self.atts[att]
                if att not in el.atts.keys():
                    v2 = '[NA]'
                    color = 'yellow'
                else:
                    v2 = el.atts[att]
                    if v2==v1:
                        color = 'green'
                    else:
                        color = 'red'
                print colored("first:\t%s = %s" % ( att, v1 ), color)
                print colored("second:\t%s = %s" % ( att, v2 ), color)
    
            for subName in self.subs.keys():
                if subName not in el.subs.keys():
                    print colored("first:\thas got %s" % ( subName), 'purple')
                    print colored("second:\thasn't got %s" % ( subName), 'purple')
                else:
                    self.subs[subName].compare( el.subs[subName] )
    
    
    
    e1 = Element(root1)
    e2 = Element(root2)
    
    e1.compare(e2)
    

提交回复
热议问题