• Python xml模块


    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打印生成格式。

  • 相关阅读:
    JavaScript
    CSS
    HTML5&CSS
    I2C mux和复杂拓扑
    如何实例化I2C设备
    SMBus 协议
    I2C 协议
    I2C和SMBus简介
    ubuntu20.04系统下更新Raspberry Pi4的DTB
    通过configfs配置的Linux USB gadget
  • 原文地址:https://www.cnblogs.com/xiangsikai/p/7787114.html
Copyright © 2020-2023  润新知