【问题标题】:Compare two xml files with good performances using python [closed]使用python比较两个性能良好的xml文件[关闭]
【发布时间】:2020-11-05 15:12:12
【问题描述】:

我想使用 Python 比较 2 个 xml 并获得特定的输出。

示例:

old.xml

<foos>
    <foo>
        <id>1</id>
        <x>1</x>
    </foo>
    <foo>
        <id>2</id>
        <x>1</x>
    </foo>
    <foo>
        <id>3</id>
        <x>1</x>
        <y>1</y>
    </foo>
</foo>

new.xml

<foos>
    <foo>
        <id>1</id>
        <x>2</x>
        <y>1</y>
    </foo>
    <foo>
        <id>2</id>
        <x>1</x>
    </foo>
    <foo>
        <id>3</id>
        <x>2</x>
        <y>1</y>
    </foo>
    <foo>
        <id>4</id>
        <x>1</x>
    </foo>
</foo>

还有我想要的输出:

output.xml

<foos>
    <foo>
        <id>1</id>
        <x>2</x>
        <y>1</y>
    </foo>
    <foo>
        <id>3</id>
        <x>2</x>
    </foo>
    <foo>
        <id>4</id>
        <x>1</x>
    </foo>
</foo>

我写了一个性能很差的非常丑陋的函数,我想找到一个更好的方法来做到这一点。您对如何以良好的表现执行此任务有任何想法吗?

我遇到的一些问题;

  • 2个xml的ids列表不相等(2个xml之间可以删除或添加对象)
  • 输出的特定格式,阻止我使用经典库来完成这项工作 (https://github.com/Shoobx/xmldiff)。但也许有解决方法?

【问题讨论】:

    标签: python xml performance compare xmldiff


    【解决方案1】:

    也许这也是一种丑陋的方法,供您参考。

    import io
    from simplified_scrapy import SimplifiedDoc, utils
    
    def getChange(oldFile='old.xml', newFile='new.xml'):
        xmlOld = utils.getFileContent(oldFile)
        docOld = SimplifiedDoc(xmlOld)
        foo = docOld.selects('foo')
        dic = {}
        for f in foo:
            dic[f.id.text] = (f.x.text, f.y.text)
    
        xmlNew = utils.getFileContent(newFile)
        docNew = SimplifiedDoc(xmlNew)
        foo = docNew.selects('foo')
        change = {}
        for f in foo:
            old = dic.get(f.id.text)
            if not old:
                change[f.id.text] = (f.x.text, f.y.text)
            else:
                new = (f.x.text, f.y.text)
                if old[0] != new[0] and old[1] != new[1]:
                    change[f.id.text] = (f.x.text, f.y.text)
                elif old[0] != new[0]:
                    change[f.id.text] = (f.x.text, '')
                elif old[1] != new[1]:
                    change[f.id.text] = ('', f.y.text)
        return change
    
    
    def saveFile(change, output='output.xml'):
        with io.open(output, mode='w') as file:
            file.write(u'<foos>\n')
            for k, v in change.items():
                file.write('<foo><id>{}</id>'.format(k))
                if v[0]:
                    file.write('<x>{}</x>'.format(v[0]))
                if v[1]:
                    file.write('<y>{}</y>'.format(v[1]))
                file.write('</foo>\n')
            file.write('</foos>\n')
    
    
    saveFile(getChange())
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-17
      • 2010-09-06
      • 1970-01-01
      • 2010-12-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多