Python xml模塊
阿新 • • 發佈:2017-11-05
alex 1.0 import back 需要 對象 left -s per
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打印生成格式。
Python xml模塊