python xml操作
1、创建xml
import xml.etree.ElementTree as et
root = et.Element('all') name = et.SubElement(root,'name') sex = et.SubElement(name,'sex') age = et.SubElement(name,'age') sex.text ='m' age.text = '20' name1 = et.SubElement(root,'name') sex1 = et.SubElement(name1,'sex') age1 = et.SubElement(name1,'age') sex1.text ='m' age1.text = '20' newxml = et.ElementTree(root) newxml.write('new.xml',encoding="utf-8",xml_declaration=True)
2、读取xml
import xml.etree.ElementTree as et root = et.parse('new.xml') print(root.getroot().tag) for child in root.getroot(): print(child.tag) for c in child: print('==',c.text)