Xml列表与Python比较 [英] Xml lists compare with Python

查看:84
本文介绍了Xml列表与Python比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经解析了这样的XML文件:



I have parsed XML file looking like this:

 <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE raml SYSTEM 'raml20.dtd'>
    <raml version="2.0" xmlns="raml20.xsd">
    <cmData type="actual">
        <managedObject class="LN" distName="PTR" id="2425">
          <p name="aak">220</p>
          <p name="orp">05</p>
          <p name="name">Portro</p>
          <p name="optres">false</p>
          <p name="optblu">false</p>
          <p name="aoptdet">false</p>
          <p name="advcell">false</p>
            <list name="sibList">
            <item>
              <p name="sibcity">177</p>
              <p name="sibrep">2</p>
            </item>
            <item>
              <p name="sibcity">177</p>
              <p name="sibrep">1</p>
            </item>
          </list>
         </managedObject>
       <managedObject class="LN" distName="KRNS" id="93886">
          <p name="aak">150</p>
          <p name="orp">05</p>
          <p name="name">Portro</p>
          <p name="optres">false</p>
          <p name="optblu">tru</p>
          <p name="aoptdet">false</p>
          <p name="advcell">true</p>
        <list name="sibList">
            <item>
              <p name="sibcity">177</p>
              <p name="sibrep">1</p>
            </item>
            <item>
              <p name="sibcity">180</p>
              <p name="sibrep">2</p>
            </item>
          </list>
    </menagedObject>
    ....
    <menagedObject>
    ...
    </menagedObject>
    ...
</cmData>
</raml>





我需要浏览所有menagedObject并比较所有参数(p名称)。我为比较编写了代码,但我不知道如何浏览列表并比较参数,如果它们具有相同的值,则不执行任何操作,如果它们不同,则输出不同的参数和值。我写了这个函数,其中键是p name,值是p name的值:



我尝试过:





I need to go through all menagedObject and compare all parametars(p name). I wrote code for comparasion, but I don't know how to go through list and compare parametars, if they have same value, do nothing, if it's different, give output of different parameters and value. I wrote this function, where key is "p name" and value is value of "p name":

What I have tried:

temp = []
for i in temp_ln:
    for j, k in zip(i.getchildren(), i):
        temp.append([i.get('distName'), j.get('name'), j.text])

    tempdict = {}
    for i in temp_ln:
        td = {}
        for j in i.getchildren():
            td.update({j.get('name'): j.text})
        tempdict.update({i.get('distName'): td})

        main_dif = {}
        for key, value in tempdict.iteritems():
            dif_k = {}
            for k, v in value.iteritems():
                try: 
                    a = ref[k]
                except:
                    a = None
                if v != a:
                    if k == 'name':
                        pass
                    else:
                        dif_k.update({k:(v, a)})
            main_dif.update({key:dif_k})

推荐答案

我用xml文件搞错了,看起来不像那样,丢失了以下全部。



I made mistake with xml file, it doesn't look like that, missing all below.

<?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE raml SYSTEM 'raml20.dtd'>
    <raml version="2.0" xmlns="raml20.xsd">
    <cmData type="actual">
        <managedObject class="LN" distName="PTR" id="2425">
          <p name="aak">220</p>
          <p name="orp">05</p>
          <p name="name">Portro</p>
          <p name="optres">false</p>
          <p name="optblu">false</p>
          <p name="aoptdet">false</p>
          <p name="advcell">false</p>
            <list name="sibList">
            <item>
              <p name="sibcity">177</p>
              <p name="sibrep">2</p>
            </item>
            <item>
              <p name="sibcity">177</p>
              <p name="sibrep">1</p>
            </item>
          
         
       <managedObject class="LN" distName="KRNS" id="93886">
          <p name="aak">150</p>
          <p name="orp">05</p>
          <p name="name">Portro</p>
          <p name="optres">false</p>
          <p name="optblu">tru</p>
          <p name="aoptdet">false</p>
          <p name="advcell">true</p>
        <list name="sibList">
            <item>
              <p name="sibcity">177</p>
              <p name="sibrep">1</p>
            </item>
            <item>
              <p name="sibcity">180</p>
              <p name="sibrep">2</p>
            </item>
          
    
    ....
    <menagedobject>
    ...
    </menagedobject>
    ...


这篇关于Xml列表与Python比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆