Python處理xml--Apple的學(xué)習(xí)筆記

一 xml數(shù)據(jù)結(jié)構(gòu)

<data info="student">
    <name>小明</name>
    <age>18</age>
    <gender>男</gender>
</data>

上述就是一個(gè)簡(jiǎn)單的xml數(shù)據(jù),這里有幾個(gè)概念,包括:

根元素 <data>
子元素 <name><age>…
標(biāo)簽 帶<>都是標(biāo)簽<data>是開始標(biāo)簽,</data>是結(jié)束標(biāo)簽
屬性 開始標(biāo)簽中有其它信息的是屬性如data標(biāo)簽中的info
文本 被開始標(biāo)簽和結(jié)束標(biāo)簽包含的是文本,如小明

二 入門實(shí)驗(yàn)代碼

    #try:  #采用底層為c的庫(kù),速度快,內(nèi)存占用小
    import xml.etree.cElementTree as ET
    #except ImportError:
    #import xml.etree.ElementTree as ET
    
    #doc = ET.parse('test.xml')
    # 打開xml文檔
    doc = ET.ElementTree(file='test.xml')
    root = doc.getroot()
    print(root.tag)
    print( root.attrib)
    print("study1")
    for child_of_root in root:
        print(child_of_root.tag,child_of_root.attrib,child_of_root.text)
    print("study2")
    print(root[0].tag, root[0].text)
    print("study3")
    for elem in doc.iter():
        print(elem.tag, elem.attrib)
    print("study4")
    for elem in doc.iter(tag="branch"):
        print(elem.tag, elem.attrib)
    print("study5")
    # 查詢
    for elem in doc.iterfind('branch/sub-branch'):
        print(elem.tag, elem.attrib)
    print("study6")
    for elem in doc.iterfind('branch[@name="release01"]'):
        print(elem.tag, elem.attrib)
    print("study7")
    # 若key存在則是修改,否則為添加屬性
    root[0].set('name', 'Apple3')
    print(root[0].attrib)
    # 刪除屬性
    del root[0].attrib['name']
    print(root[0].attrib)
    for subelem in root:
        print(subelem.tag, subelem.attrib)
    print("study8")
    # 將修改寫入xml文檔
    doc.write('test.xml')
    print("write ok")

三 實(shí)驗(yàn)輸出結(jié)果

doc
{}
study1
branch {'foo': 'bar', 'hash': '1cdf045c', 'name2': 'Apple2'} 
    text,source
  
branch {'hash': 'f200013e', 'name': 'release01'} 
    
branch {'name': 'invalid'} 
  
study2
branch 
    text,source
  
study3
doc {}
branch {'foo': 'bar', 'hash': '1cdf045c', 'name2': 'Apple2'}
branch {'hash': 'f200013e', 'name': 'release01'}
sub-branch {'name': 'subrelease01'}
branch {'name': 'invalid'}
study4
branch {'foo': 'bar', 'hash': '1cdf045c', 'name2': 'Apple2'}
branch {'hash': 'f200013e', 'name': 'release01'}
branch {'name': 'invalid'}
study5
sub-branch {'name': 'subrelease01'}
study6
branch {'hash': 'f200013e', 'name': 'release01'}
study7
{'foo': 'bar', 'hash': '1cdf045c', 'name2': 'Apple2', 'name': 'Apple3'}
{'foo': 'bar', 'hash': '1cdf045c', 'name2': 'Apple2'}
branch {'foo': 'bar', 'hash': '1cdf045c', 'name2': 'Apple2'}
branch {'hash': 'f200013e', 'name': 'release01'}
branch {'name': 'invalid'}
study8
ok

四 參考網(wǎng)址:

1.  https://blog.csdn.net/wklken/article/details/7603071?utm_source=blogxgwz7
2.  https://blog.csdn.net/weixin_30549657/article/details/98340427
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 一. Java基礎(chǔ)部分.................................................
    wy_sure閱讀 4,017評(píng)論 0 11
  • Swift1> Swift和OC的區(qū)別1.1> Swift沒有地址/指針的概念1.2> 泛型1.3> 類型嚴(yán)謹(jǐn) 對(duì)...
    cosWriter閱讀 11,675評(píng)論 1 32
  • 一直以來(lái),自己是一個(gè)容易焦慮的人,有壓力時(shí)會(huì)吃很多東西,希望自己做到最好,做不好時(shí)自己會(huì)失落,會(huì)沮喪。 對(duì)孩子的教...
    AnnaFan閱讀 243評(píng)論 1 0
  • 3c940cf391b8閱讀 174評(píng)論 0 4
  • 1月份因?yàn)樽x書、集市分享,以及喜馬拉雅分享都有所突破,所以1月份的關(guān)鍵詞是突破,當(dāng)然我相信未來(lái)還會(huì)有更多的突破。 ...
    靜心成長(zhǎng)靜姐閱讀 892評(píng)論 2 13

友情鏈接更多精彩內(nèi)容