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

python讀取xml文件方法解析

 更新時(shí)間:2020年08月04日 10:46:27   作者:蟲師  
這篇文章主要介紹了python讀取xml文件方法解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

  關(guān)于python讀取xml文章很多,但大多文章都是貼一個(gè)xml文件,然后再貼個(gè)處理文件的代碼。這樣并不利于初學(xué)者的學(xué)習(xí),希望這篇文章可以更通俗易懂的教如何使用python來讀取xml文件。

什么是xml?

xml即可擴(kuò)展標(biāo)記語言,它可以用來標(biāo)記數(shù)據(jù)、定義數(shù)據(jù)類型,是一種允許用戶對(duì)自己的標(biāo)記語言進(jìn)行定義的源語言。

abc.xml

<?xml version="1.0" encoding="utf-8"?>
<catalog>
  <maxid>4</maxid>
  <login username="pytest" passwd='123456'>
    <caption>Python</caption>
    <item id="4">
      <caption>測試</caption>
    </item>
  </login>
  <item id="2">
    <caption>Zope</caption>
  </item>
</catalog>

Ok,從結(jié)構(gòu)上,它很像我們常見的HTML超文本標(biāo)記語言。但他們被設(shè)計(jì)的目的是不同的,超文本標(biāo)記語言被設(shè)計(jì)用來顯示數(shù)據(jù),其焦點(diǎn)是數(shù)據(jù)的外觀。它被設(shè)計(jì)用來傳輸和存儲(chǔ)數(shù)據(jù),其焦點(diǎn)是數(shù)據(jù)的內(nèi)容。

那么它有如下特征:

首先,它是有標(biāo)簽對(duì)組成,<aa></aa>

標(biāo)簽可以有屬性:<aaid='123'></aa>

標(biāo)簽對(duì)可以嵌入數(shù)據(jù):<aa>abc</aa>

標(biāo)簽可以嵌入子標(biāo)簽(具有層級(jí)關(guān)系):

<aa>

<bb></bb>

</aa>

獲得標(biāo)簽屬性

那么,下面來介紹如何用python來讀取這種類型的文件。

#coding=utf-8
import xml.dom.minidom

#打開xml文檔
dom = xml.dom.minidom.parse('abc.xml')

#得到文檔元素對(duì)象
root = dom.documentElement
print root.nodeName
print root.nodeValue
print root.nodeType
print root.ELEMENT_NODE

mxl.dom.minidom模塊被用來處理xml文件,所以要先引入。

xml.dom.minidom.parse()用于打開一個(gè)xml文件,并將這個(gè)文件對(duì)象dom變量。

documentElement用于得到dom對(duì)象的文檔元素,并把獲得的對(duì)象給root

每一個(gè)結(jié)點(diǎn)都有它的nodeName,nodeValue,nodeType屬性。

nodeName為結(jié)點(diǎn)名字。

nodeValue是結(jié)點(diǎn)的值,只對(duì)文本結(jié)點(diǎn)有效。

nodeType是結(jié)點(diǎn)的類型。catalog是ELEMENT_NODE類型

現(xiàn)在有以下幾種:

'ATTRIBUTE_NODE'
'CDATA_SECTION_NODE'
'COMMENT_NODE'
'DOCUMENT_FRAGMENT_NODE'
'DOCUMENT_NODE'
'DOCUMENT_TYPE_NODE'
'ELEMENT_NODE'
'ENTITY_NODE'
'ENTITY_REFERENCE_NODE'
'NOTATION_NODE'
'PROCESSING_INSTRUCTION_NODE'
'TEXT_NODE'

NodeTypes-有名常數(shù)

http://www.w3school.com.cn/xmldom/dom_nodetype.asp

獲得子標(biāo)簽

現(xiàn)在要獲得catalog的子標(biāo)簽以的標(biāo)簽name

<?xml version="1.0" encoding="utf-8"?>
<catalog>
    <maxid>4</maxid>
    <login username="pytest" passwd='123456'>
        <caption>Python</caption>
       <item id="4">
          <caption>測試</caption>
      </item>
  </login>
  <item id="2">
      <caption>Zope</caption>
  </item>
</catalog>

對(duì)于知道元素名字的子元素,可以使用getElementsByTagName方法獲?。?/p>

<?xml version="1.0" encoding="utf-8"?>
<catalog>
    <maxid>4</maxid>
    <login username="pytest" passwd='123456'>
        <caption>Python</caption>
       <item id="4">
          <caption>測試</caption>
      </item>
  </login>
  <item id="2">
      <caption>Zope</caption>
  </item>
</catalog>

如何區(qū)分相同標(biāo)簽名字的標(biāo)簽:

<?xml version="1.0" encoding="utf-8"?>
<catalog>
    <maxid>4</maxid>
    <login username="pytest" passwd='123456'>
        <caption>Python</caption>
       <item id="4">
          <caption>測試</caption>
      </item>
  </login>
  <item id="2">
      <caption>Zope</caption>
  </item>
</catalog>

<caption>和<item>標(biāo)簽不止一個(gè)如何區(qū)分?

#coding=utf-8
import xml.dom.minidom

#打開xml文檔
dom = xml.dom.minidom.parse('abc.xml')

#得到文檔元素對(duì)象
root = dom.documentElement

bb = root.getElementsByTagName('caption')
b= bb[2]
print b.nodeName

bb = root.getElementsByTagName('item')
b= bb[1]
print b.nodeName

root.getElementsByTagName('caption')獲得的是標(biāo)簽為caption一組標(biāo)簽,b[0]表示一組標(biāo)簽中的第一個(gè);b[2],表示這一組標(biāo)簽中的第三個(gè)。

獲得標(biāo)簽屬性值

<?xml version="1.0" encoding="utf-8"?>
<catalog>
    <maxid>4</maxid>
    <login username="pytest" passwd='123456'>
        <caption>Python</caption>
       <item id="4">
          <caption>測試</caption>
      </item>
  </login>
  <item id="2">
      <caption>Zope</caption>
  </item>
</catalog>

<login>和<item>標(biāo)簽是有屬性的,如何獲得他們的屬性?

#coding=utf-8
import xml.dom.minidom

#打開xml文檔
dom = xml.dom.minidom.parse('abc.xml')

#得到文檔元素對(duì)象
root = dom.documentElement

itemlist = root.getElementsByTagName('login')
item = itemlist[0]
un=item.getAttribute("username")
print un
pd=item.getAttribute("passwd")
print pd

ii = root.getElementsByTagName('item')
i1 = ii[0]
i=i1.getAttribute("id")
print i

i2 = ii[1]
i=i2.getAttribute("id")
print i

getAttribute方法可以獲得元素的屬性所對(duì)應(yīng)的值。

獲得標(biāo)簽對(duì)之間的數(shù)據(jù)

<?xml version="1.0" encoding="utf-8"?>
<catalog>
    <maxid>4</maxid>
    <login username="pytest" passwd='123456'>
        <caption>Python</caption>
       <item id="4">
          <caption>測試</caption>
      </item>
  </login>
  <item id="2">
      <caption>Zope</caption>
  </item>
</catalog>

<caption>標(biāo)簽對(duì)之間是有數(shù)據(jù)的,如何獲得這些數(shù)據(jù)?

獲得標(biāo)簽對(duì)之間的數(shù)據(jù)有多種方法,

方法一

#coding=utf-8
import xml.dom.minidom

#打開xml文檔
dom = xml.dom.minidom.parse('abc.xml')

#得到文檔元素對(duì)象
root = dom.documentElement

cc=dom.getElementsByTagName('caption')
c1=cc[0]
print c1.firstChild.data

c2=cc[1]
print c2.firstChild.data

c3=cc[2]
print c3.firstChild.data

firstChild屬性返回被選節(jié)點(diǎn)的第一個(gè)子節(jié)點(diǎn),.data表示獲取該節(jié)點(diǎn)人數(shù)據(jù)。

方法二

#coding=utf-8
from xml.etree import ElementTree as ET
per=ET.parse('abc.xml')
p=per.findall('./login/item')

for oneper in p:
  for child in oneper.getchildren():
    print child.tag,':',child.text


p=per.findall('./item')

for oneper in p:
  for child in oneper.getchildren():
    print child.tag,':',child.text

方法二有點(diǎn)復(fù)雜,所引用模塊也與前面的不一樣,findall用于指定在哪一級(jí)標(biāo)簽下開始遍歷。

getchildren方法按照文檔順序返回所有子標(biāo)簽。并輸出標(biāo)簽名(child.tag)和標(biāo)簽的數(shù)據(jù)(child.text)

其實(shí),方法二的作用不在于此,它核心功能是可以遍歷某一級(jí)標(biāo)簽下的所有子標(biāo)簽。

到此這篇關(guān)于python讀取xml文件方法解析的文章就介紹到這了,更多相關(guān)python讀取xml文件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論