標籤:學習日誌 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模組