标签:学习日志 log ati find 2.x attr 之间 try import
1)XML模块
xml是实现不同语言或程序之间进行数据交换的协议,跟json差不多。
下面是xml的遍历查询删除修改和生成
# -*- coding:utf-8 -*- __author__ = ‘shisanjun‘ import xml.etree.ElementTree as ET etree=ET.parse("xml.xml") root=etree.getroot() #遍历XML for 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模块
标签:学习日志 log ati find 2.x attr 之间 try import
原文地址:http://www.cnblogs.com/lixiang1013/p/6852037.html