欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python比較2個(gè)xml內(nèi)容的方法

 更新時(shí)間:2015年05月11日 16:12:36   作者:像風(fēng)一樣的自由  
這篇文章主要介紹了python比較2個(gè)xml內(nèi)容的方法,涉及Python操作XML文件的相關(guān)技巧,需要的朋友可以參考下

本文實(shí)例講述了python比較2個(gè)xml內(nèi)容的方法。分享給大家供大家參考。具體分析如下:

from xml.etree import ElementTree 
OK=True 
main_pid = 10000 
loop_depth = 0 
def compare_xml(left, right, key_info='.'):
  global loop_depth 
  loop_depth += 1 
  if loop_depth == 1: print 
  if left.tag != right.tag: 
    print_diff(main_pid, key_info, 'difftag', left.tag, right.tag)
    return 
  if left.text != right.text: 
    print_diff(main_pid, key_info, 'difftext', left.text, right.text)
    return 
  leftitems = dict(left.items()) 
  rightitems = dict(right.items()) 
  for k,v in leftitems.items(): 
    if k not in rightitems: 
      s = '%s/%s' % (key_info, left.tag) 
      print_diff(main_pid, s, 'lostattr', k, "")
  for k,v in rightitems.items(): 
    if k not in leftitems: 
      s = '%s/%s' % (key_info, right.tag) 
      print_diff(main_pid, s, 'extraattr', "", k)
  leftnodes = left.getchildren() 
  rightnodes = right.getchildren() 
  leftlen = len(leftnodes) 
  rightlen = len(rightnodes) 
  if leftlen != rightlen: 
    s = '%s/%s' % (key_info, right.tag)
    print_diff(main_pid, s, 'difflen', leftlen, rightlen)
    return 
  l = leftlen<rightlen and leftlen or rightlen
  d = {} 
  for i in xrange(l):     
    node=leftnodes[i] 
    if node.tag not in d: 
      d[node.tag] = 1 
      tag = node.tag 
    else: 
      tag = node.tag + str(d[node.tag])
      d[node.tag] += 1 
    s = '%s/%s' % (key_info, tag) 
    compare_xml(leftnodes[i], rightnodes[i], s)
def print_diff(main_pid, key_info, msg, base_type, test_type):
  global OK 
  info = u'[ %-5s ] %s -> %-40s [ %s != %s ]'%(msg.upper(), main_pid, key_info.strip('./'), base_type, test_type)
  print info.encode('gbk')
  OK = False

調(diào)用:

if __name__ == '__main__': 
  s1 = '''''<?xml version="1.0" encoding="UTF-8"?> \
     <employees> \ 
     <employee id = '1'> \ 
      <name>linux</name>\ 
      <age>30</age>\ 
     </employee>\ 
     <employee id = '2'> \ 
      <name>windows</name>\ 
      <age>20</age>\ 
     </employee>\ 
     </employees>'''  
  s2 = '''''<?xml version="1.0" encoding="UTF-8"?> \
     <employees> \ 
     <employee id = '3'> \ 
      <name>windows</name>\ 
      <age>20</age>\ 
     </employee>\ 
     <employee id = '4'> \ 
      <name>linux</name>\ 
      <age>30</age>\ 
     </employee>\ 
     </employees>'''  
  lroot = ElementTree.fromstring(s1) 
  rroot = ElementTree.fromstring(s2) 
  compare_xml(lroot, rroot)

希望本文所述對(duì)大家的Python程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • Python中類的mro與繼承關(guān)系詳解(二)

    Python中類的mro與繼承關(guān)系詳解(二)

    這篇文章主要介紹了Python中類的mro與繼承關(guān)系詳解,上一篇我們已經(jīng)通過mro相關(guān)資料介紹了mro繼承關(guān)系的基礎(chǔ)內(nèi)容,這片緊接著上一篇文章展開詳細(xì)內(nèi)容,需要的朋友可以參考一下
    2022-07-07
  • Python使用列表推導(dǎo)式快速生成列表

    Python使用列表推導(dǎo)式快速生成列表

    這篇文章主要介紹了Python使用列表推導(dǎo)式快速生成列表,列表推導(dǎo)式是Python構(gòu)建列表list的一種快捷方式,可以使用簡潔的代碼就創(chuàng)建出一個(gè)列表,需要的朋友可以參考下
    2023-07-07
  • python3 kmp 字符串匹配的方法

    python3 kmp 字符串匹配的方法

    這篇文章主要介紹了python3 kmp 字符串匹配的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-07-07
  • 解決Tensorflow sess.run導(dǎo)致的內(nèi)存溢出問題

    解決Tensorflow sess.run導(dǎo)致的內(nèi)存溢出問題

    今天小編就為大家分享一篇解決Tensorflow sess.run導(dǎo)致的內(nèi)存溢出問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-02-02
  • python實(shí)現(xiàn)大轉(zhuǎn)盤抽獎(jiǎng)效果

    python實(shí)現(xiàn)大轉(zhuǎn)盤抽獎(jiǎng)效果

    這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)大轉(zhuǎn)盤抽獎(jiǎng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-01-01
  • python?pygame英雄循環(huán)飛行及作業(yè)示例

    python?pygame英雄循環(huán)飛行及作業(yè)示例

    這篇文章主要為大家介紹了python?pygame英雄循環(huán)飛行及作業(yè)實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-08-08
  • Python使用20行代碼實(shí)現(xiàn)微信聊天機(jī)器人

    Python使用20行代碼實(shí)現(xiàn)微信聊天機(jī)器人

    這篇文章主要介紹了Python使用20行代碼實(shí)現(xiàn)微信聊天機(jī)器人,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-06-06
  • PHP webshell檢查工具 python實(shí)現(xiàn)代碼

    PHP webshell檢查工具 python實(shí)現(xiàn)代碼

    Web安全應(yīng)急響應(yīng)中,不免要檢查下服務(wù)器上是否被上傳了webshell,手工檢查比較慢,就寫了個(gè)腳本來檢查了。Windows平臺(tái)下已經(jīng)有了lake2寫的雷克圖的了,一般的檢查也夠用了,寫了個(gè)Linux下面的,用python寫的。
    2009-09-09
  • Python實(shí)現(xiàn)判斷一個(gè)字符串是否包含子串的方法總結(jié)

    Python實(shí)現(xiàn)判斷一個(gè)字符串是否包含子串的方法總結(jié)

    這篇文章主要介紹了Python實(shí)現(xiàn)判斷一個(gè)字符串是否包含子串的方法,結(jié)合實(shí)例形式總結(jié)分析了四種比較常用的字符串子串判定方法,需要的朋友可以參考下
    2017-11-11
  • Python識(shí)別二維碼的兩種方法詳解

    Python識(shí)別二維碼的兩種方法詳解

    在學(xué)習(xí)Python處理二維碼的過程中,我們看到的大多是“用python生成酷炫二維碼”、“用Python制作動(dòng)圖二維碼”之類的文章。而關(guān)于使用Python批量識(shí)別二維碼的教程,并不多見。本文就為大家總結(jié)了Python識(shí)別二維碼的兩種方法,感興趣的可以學(xué)習(xí)一下
    2022-06-06

最新評(píng)論