xml.etree.ElementTree 模块 xml文件格式
import xml.etree.ElementTree as ET
xml文件格式
<?xml version="1.0"?> <data> <country name="Liechtenstein"> <rank updated="yes">2</rank> <year>2008</year> <gdppc>141100</gdppc> <neighbor name="Austria" direction="E"/> <neighbor name="Switzerland" direction="W"/> </country> <country name="Singapore"> <rank updated="yes">5</rank> <year>2011</year> <gdppc>59900</gdppc> <neighbor name="Malaysia" direction="N"/> </country> <country name="Panama"> <rank updated="yes">69</rank> <year>2011</year> <gdppc>13600</gdppc> <neighbor name="Costa Rica" direction="W"/> <neighbor name="Colombia" direction="E"/> </country> </data> xml数据
xml协议在各个语言里的都 是支持的,在python中可以用以下模块操作xml:
# print(root.iter('year')) #全文搜索
# print(root.find('country')) #在root的子节点找,只找一个
# print(root.findall('country')) #在root的子节点找,找所有
读取文件
1 import xml.etree.ElementTree as ET 2 tree = ET.parse(r'E:\python\day1\day5\b.test') 3 root = tree.getroot() 4 print (root.tag) 5 data
遍历文件country标签
1 import xml.etree.ElementTree as ET 2 tree = ET.parse(r'E:\python\day1\day5\b.test') 3 root = tree.getroot() 4 for i in root.iter('country'): #全文的 country节点 5 print (i.tag,i.text,i.attrib) #tar标签 text内容 attrib标签属性 6 7 country 8 {'name': 'Liechtenstein'} 9 country 10 {'name': 'Singapore'} 11 country 12 {'name': 'Panama'}
遍历全文
1 tree = ET.parse(r'E:\python\day1\day5\b.test') 2 root = tree.getroot() 3 for coun in root: 4 print ('=====>',coun.attrib['name']) 5 for i in coun: 6 print (i.tag,i.text,i.attrib) 7 8 =====> Liechtenstein 9 rank 2 {'updated': 'yes'} 10 year 2008 {} 11 gdppc 141100 {} 12 neighbor None {'name': 'Austria', 'direction': 'E'} 13 neighbor None {'name': 'Switzerland', 'direction': 'W'} 14 =====> Singapore 15 rank 5 {'updated': 'yes'} 16 year 2011 {} 17 gdppc 59900 {} 18 neighbor None {'name': 'Malaysia', 'direction': 'N'} 19 =====> Panama 20 rank 69 {'updated': 'yes'} 21 year 2011 {} 22 gdppc 13600 {} 23 neighbor None {'name': 'Costa Rica', 'direction': 'W'} 24 neighbor None {'name': 'Colombia', 'direction': 'E'}
修改标签内容
1 tree = ET.parse(r'E:\python\day1\day5\b.test') 2 root = tree.getroot() 3 4 for coun in root.iter('year'): 5 print (coun.text) 6 coun.text = str(int(coun.text)+1) ##year + 1 7 coun.set('update','yes') ##标签内加上 update="yes" 8 tree.write(r'E:\python\day1\day5\b.test') ##写入文件
删除节点,只能逐级删
条件 删除 year的值 大于2010
1 tree = ET.parse(r'E:\python\day1\day5\b.test') 2 root = tree.getroot() 3 4 for country in root: #遍历文件country标签 5 print (country.find('year')) 在country标签内找year标签 6 year = country.find('year') 7 if int(year.text) > 2010: 8 country.remove(year) #删除country 内的 year标签 9 tree.write(r'E:\python\day1\day5\a.test')
添加节点
1 for country in root: 2 print (country.find('year')) 3 year = country.find('year') 4 if int(year.text) > 2010: 5 year2 = ET.Element('year2') #制作标签 6 year2.text = str(int(year.text)+1) 7 year2.set('yinhang','SB') 8 country.append(year2) #添加标签 9 tree.write(r'E:\python\day1\day5\a.test')
新建 xml文件
1 import xml.etree.ElementTree as ET 2 3 4 new_xml = ET.Element("namelist") #创建主标签 5 name = ET.SubElement(new_xml,"name",attrib={"enrolled":"yes"}) #创建子标签 参数1在哪里创建子标签 参数2:标签名字 参数3:标签属性 6 age = ET.SubElement(name,"age",attrib={"checked":"no"}) 7 sex = ET.SubElement(name,"sex") 8 sex.text = '33' #子标签内容 9 name2 = ET.SubElement(new_xml,"name",attrib={"enrolled":"no"}) 10 age = ET.SubElement(name2,"age") 11 age.text = '19' 12 13 et = ET.ElementTree(new_xml) #生成文档对象 14 et.write("test.xml", encoding="utf-8",xml_declaration=True) ##xml_declaration 文件头声明 15 16 ET.dump(new_xml) #打印生成的格式