国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

腳本之家,腳本語言編程技術及教程分享平臺!
分類導航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務器之家 - 腳本之家 - Python - python自定義解析簡單xml格式文件的方法

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

2020-06-27 11:39像風一樣的自由 Python

這篇文章主要介紹了python自定義解析簡單xml格式文件的方法,涉及Python解析XML文件的相關技巧,非常具有實用價值,需要的朋友可以參考下

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

因為公司內部的接口返回的字串支持2種形式:php數組,xml;結果php數組python不能直接用,而xml字符串的格式不是標準的,所以也不能用標準模塊解析。【不標準的地方是某些節點會的名稱是以數字開頭的】,所以寫個簡單的腳步來解析一下文件,用來做接口測試。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/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

運行結果:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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格式比較簡單,沒有帶屬性的節點,所以處理起來就比較簡單了。但測試還是發現有一個bug。即當相同節點嵌套時會出現正則匹配出問題,該問題的可以通過避免在xpath中出現有嵌套節點的名稱來解決,否則只有重寫復雜的機制了。

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 人人澡人人射 | 国产精品久久久久久久久久久小说 | 欧美一级在线观看 | 91精品国产综合久久久蜜臀粉嫩 | av最新在线 | 在线观看一区二区三区视频 | 日韩精品一区二区三区在线观看 | 亚洲精品国产片 | 欧美大片一区 | 亚洲精品久久 | 欧美视频在线免费 | 日本激情视频一区二区三区 | 国产精品久久久久久久9999 | 精品视频在线播放 | 天天操夜夜爽 | 国产一区免费视频 | 狠狠撸在线 | 人成网址 | 亚洲激情在线 | 国产精品欧美日韩在线观看 | 激情综合色综合久久综合 | 成人午夜精品久久久久久久3d | 久久久久久久免费观看 | 中文字幕乱码一区二区三区 | 日韩欧美国产一区二区三区 | 亚洲人成在线播放 | 久久久久久久久久久免费 | 久久综合久 | 日本精品一区二区三区在线观看视频 | 成人自拍视频 | 看亚洲a级一级毛片 | 国产欧美综合一区二区三区 | 国产一区二区在线免费观看 | 欧美一级二级视频 | 精品在线一区 | 中文区永久区 | 亚洲一区在线日韩在线深爱 | 亚洲电影在线 | 日韩aaa视频 | 亚洲成人久久久 | 成人欧美一区二区三区视频xxx |