xml模块 |
自己创建xml文档
import xml.etree.cElementTree as ET new_xml = ET.Element("personinfolist") personinfo = ET.SubElement(new_xml,"personinfo",attrib={"enrolled":"yes"}) name = ET.SubElement(personinfo,"name") name.text = "Alex Li" age = ET.SubElement(personinfo,"age",attrib={"checked":"no"}) sex = ET.SubElement(personinfo,"sex") age.text = '56' et = ET.ElementTree(new_xml) et.write("test1.xml",encoding="utf-8",xml_declaration=True) ET.dump(new_xml)
结果
<?xml version='1.0' encoding='utf-8'?> <personinfolist> <personinfo enrolled="yes"> <name>Alex Li</name> <age checked="no">56</age><sex /> </personinfo> </personinfolist>
注:创建需要启用xml模块。
注:创建时需要用到ET.Element创建名
注:ET.SubElement创建其他值。
注:ET.ElementTree生成文档对象。
注:ET.dump打印生成格式。