python基礎學習日誌day5---xml和configparse模組

來源:互聯網
上載者:User

標籤:學習日誌   log   ati   find   2.x   attr   之間   try   import   

1)XML模組

xml是實現不同語言或程式之間進行資料交換的協議,跟json差不多。

下面是xml的遍曆查詢刪除修改和產生

# -*- coding:utf-8 -*-__author__ = ‘shisanjun‘import xml.etree.ElementTree as ETetree=ET.parse("xml.xml")root=etree.getroot()#遍曆XMLfor child in root:    print(child.tag,child.attrib,child.text)    for i in child:        print("\t",i.tag,i.attrib,i.text)#查詢for child in root.iter("year"):    print(child.tag,child.text)#修改和刪除xml文檔內容for country in root.findall("country"):    rank=country.find("rank").text    if int(rank)>50:        root.remove(country)etree.write("xml1.xml")for year in root.iter("year"):    year_tmp=int(year.text)+1    year.text=str(year_tmp)    year.set("update","yes")etree.write("xml2.xml")#建立XML檔案new_xml=ET.Element("root") #產生根name=ET.SubElement(new_xml,"name",attrib={"errol":"2013"})age=ET.SubElement(name,"age")age.text=str(23)sex=ET.SubElement(name,"sex")sex.text="boy"et=ET.ElementTree(new_xml)et.write("xml3.xml",encoding="utf-8",xml_declaration=True)#寫入檔案ET.dump(new_xml) #列印到終端

 

python基礎學習日誌day5---xml和configparse模組

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.