最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python自定義解析簡單xml格式文件的方法

 更新時間:2015年05月11日 15:51:26   作者:像風(fēng)一樣的自由  
這篇文章主要介紹了python自定義解析簡單xml格式文件的方法,涉及Python解析XML文件的相關(guān)技巧,非常具有實用價值,需要的朋友可以參考下

本文實例講述了python自定義解析簡單xml格式文件的方法。分享給大家供大家參考。具體分析如下:

因為公司內(nèi)部的接口返回的字串支持2種形式:php數(shù)組,xml;結(jié)果php數(shù)組python不能直接用,而xml字符串的格式不是標(biāo)準(zhǔn)的,所以也不能用標(biāo)準(zhǔn)模塊解析?!静粯?biāo)準(zhǔn)的地方是某些節(jié)點(diǎn)會的名稱是以數(shù)字開頭的】,所以寫個簡單的腳步來解析一下文件,用來做接口測試。

#!/usr/bin/env python
#encoding: utf-8
import re
class xmlparse:
  def __init__(self, xmlstr):
    self.xmlstr = xmlstr
    self.xmldom = self.__convet2utf8()
    self.xmlnodelist = []
    self.xpath = ''
  def __convet2utf8(self):
    headstr = self.__get_head()
    xmldomstr = self.xmlstr.replace(headstr, '')
    if 'gbk' in headstr: 
      xmldomstr = xmldomstr.decode('gbk').encode('utf-8')
    elif 'gb2312' in headstr:
      xmldomstr = self.xmlstr.decode('gb2312').encode('utf-8')
    return xmldomstr
  def __get_head(self):
    headpat = r'<\?xml.*\?>'
    headpatobj = re.compile(headpat)
    headregobj = headpatobj.match(self.xmlstr)
    if headregobj:
      headstr = headregobj.group()
      return headstr
    else:
      return ''
  def parse(self, xpath):
    self.xpath = xpath
    xpatlist = []
    xpatharr = self.xpath.split('/')
    for xnode in xpatharr:
      if xnode:
        spcindex = xnode.find('[')
        if spcindex > -1:
          index = int(xnode[spcindex+1:-1])
          xnode = xnode[:spcindex]
        else:
          index = 0;
        temppat = ('<%s>(.*?)</%s>' % (xnode, xnode),index)
        xpatlist.append(temppat)
    xmlnodestr = self.xmldom
    for xpat,index in xpatlist:
      xmlnodelist = re.findall(xpat,xmlnodestr)
      xmlnodestr = xmlnodelist[index]
      if xmlnodestr.startswith(r'<![CDATA['):
        xmlnodestr = xmlnodestr.replace(r'<![CDATA[','')[:-3]
    self.xmlnodelist = xmlnodelist
    return xmlnodestr
if '__main__' == __name__:
  xmlstr = '<?xml version="1.0" encoding="utf-8" standalone="yes" ?><resultObject><a><product_id>aaaaa</product_id><product_name><![CDATA[bbbbb]]></a><b><product_id>bbbbb</product_id><product_name><![CDATA[bbbbb]]></b></product_name></resultObject>'
  xpath1 = '/product_id'
  xpath2 = '/product_id[1]'
  xpath3 = '/a/product_id'
  xp = xmlparse(xmlstr)
  print 'xmlstr:',xp.xmlstr
  print 'xmldom:',xp.xmldom
  print '------------------------------'
  getstr = xp.parse(xpath1)
  print 'xpath:',xp.xpath
  print 'get list:',xp.xmlnodelist
  print 'get string:', getstr
  print '------------------------------'
  getstr = xp.parse(xpath2)
  print 'xpath:',xp.xpath
  print 'get list:',xp.xmlnodelist
  print 'get string:', getstr
  print '------------------------------'
  getstr = xp.parse(xpath3)
  print 'xpath:',xp.xpath
  print 'get list:',xp.xmlnodelist
  print 'get string:', getstr

運(yùn)行結(jié)果:

xmlstr: <?xml version="1.0" encoding="utf-8" standalone="yes" ?><resultObject><a><product_id>aaaaa</product_id><product_name><![CDATA[bbbbb]]></a><b><product_id>bbbbb</product_id><product_name><![CDATA[bbbbb]]></b></product_name></resultObject>
xmldom: <resultObject><a><product_id>aaaaa</product_id><product_name><![CDATA[bbbbb]]></a><b><product_id>bbbbb</product_id><product_name><![CDATA[bbbbb]]></b></product_name></resultObject>
------------------------------
xpath: /product_id
get list: ['aaaaa', 'bbbbb']
get string: aaaaa
------------------------------
xpath: /product_id[1] 
get list: ['aaaaa', 'bbbbb']
get string: bbbbb
------------------------------
xpath: /a/product_id
get list: ['aaaaa']
get string: aaaaa

因為返回的xml格式比較簡單,沒有帶屬性的節(jié)點(diǎn),所以處理起來就比較簡單了。但測試還是發(fā)現(xiàn)有一個bug。即當(dāng)相同節(jié)點(diǎn)嵌套時會出現(xiàn)正則匹配出問題,該問題的可以通過避免在xpath中出現(xiàn)有嵌套節(jié)點(diǎn)的名稱來解決,否則只有重寫復(fù)雜的機(jī)制了。

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

相關(guān)文章

  • 淺談keras中的后端backend及其相關(guān)函數(shù)(K.prod,K.cast)

    淺談keras中的后端backend及其相關(guān)函數(shù)(K.prod,K.cast)

    這篇文章主要介紹了淺談keras中的后端backend及其相關(guān)函數(shù)(K.prod,K.cast),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-06-06
  • python實現(xiàn)apahce網(wǎng)站日志分析示例

    python實現(xiàn)apahce網(wǎng)站日志分析示例

    這篇文章主要介紹了python實現(xiàn)apahce網(wǎng)站日志分析示例,需要的朋友可以參考下
    2014-04-04
  • pytorch 求網(wǎng)絡(luò)模型參數(shù)實例

    pytorch 求網(wǎng)絡(luò)模型參數(shù)實例

    今天小編就為大家分享一篇pytorch 求網(wǎng)絡(luò)模型參數(shù)實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-12-12
  • python列表中刪除重復(fù)元素的三種方法

    python列表中刪除重復(fù)元素的三種方法

    本文主要介紹了python列表中刪除重復(fù)元素的三種方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2025-01-01
  • python使用MkDocs自動生成文檔的操作方法

    python使用MkDocs自動生成文檔的操作方法

    python代碼注釋風(fēng)格有很多,比較主流的有 reStructuredText風(fēng)格、numpy風(fēng)格、Google風(fēng)格,自動生成文檔的工具也有很多,常見的有:Pydocs,Sphinx和MkDocs,本文給大家介紹了python使用MkDocs自動生成文檔的操作方法,需要的朋友可以參考下
    2024-06-06
  • python數(shù)據(jù)可視化之初探?Seaborn

    python數(shù)據(jù)可視化之初探?Seaborn

    Seaborn?是一個基于?Matplotlib?的?Python?數(shù)據(jù)可視化庫,它提供了更高級別的接口,使得創(chuàng)建美觀的統(tǒng)計圖形變得非常簡單,在這篇文章中,我們將討論?Seaborn?的基礎(chǔ)使用方法,包括如何創(chuàng)建各種常見的統(tǒng)計圖形
    2023-07-07
  • python seaborn heatmap可視化相關(guān)性矩陣實例

    python seaborn heatmap可視化相關(guān)性矩陣實例

    這篇文章主要介紹了python seaborn heatmap可視化相關(guān)性矩陣實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-06-06
  • Python搶購腳本的編寫方法

    Python搶購腳本的編寫方法

    本文給大家分享一個秒殺搶購腳本,幫助大家雙十二搶購心愛的禮物,步驟很簡單,下面小編給大家分享基于Python搶購腳本的編寫方法,感興趣的朋友一起看看吧
    2021-11-11
  • 用python登錄Dr.com思路以及代碼分享

    用python登錄Dr.com思路以及代碼分享

    如今一般的大學(xué)校園或者公寓都是通過客戶端來限制路由器使用,基本上都是Dr.com客戶端,有的是登錄樣式,有的是插件樣式。下面我們來說說python定制自己的客戶端
    2014-06-06
  • python讀寫json文件的簡單實現(xiàn)

    python讀寫json文件的簡單實現(xiàn)

    這篇文章主要介紹了python讀寫json文件的簡單實現(xiàn),實例分析了各種讀寫json的方法和技巧,有興趣的可以了解一下
    2017-04-04

最新評論

沙坪坝区| 宣武区| 称多县| 翼城县| 宁乡县| 瑞丽市| 科技| 乌兰察布市| 石城县| 长泰县| 青铜峡市| 武威市| 太保市| 庆元县| 宜兰县| 苏尼特左旗| 临沂市| 晋中市| 昭平县| 平邑县| 长白| 延安市| 青阳县| 罗定市| 文山县| 出国| 留坝县| 镇江市| 乐至县| 井研县| 绍兴县| 积石山| 郑州市| 阆中市| 岚皋县| 略阳县| 龙井市| 卓尼县| 鲁山县| 河南省| 巴彦淖尔市|