Xml列表与Python比较

编程入门 行业动态 更新时间:2024-10-24 04:47:56
本文介绍了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比较

本文发布于:2023-10-23 14:03:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1521036.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:列表   Xml   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!