python写入xml文件的方法

作者:work24 时间:2023-01-01 15:08:54 

本文实例讲述了python写入xml文件的方法。分享给大家供大家参考。具体分析如下:

本范例通过xml模块对xml文件进行写入操作


from xml.dom.minidom import Document
doc = Document()
people = doc.createElement("people")
doc.appendChild(people)
aperson = doc.createElement("person")
people.appendChild(aperson)
name = doc.createElement("name")
aperson.appendChild(name)
personname = doc.createTextNode("Annie")
name.appendChild(personname)
filename = "people.xml"
f = open(filename, "w")
f.write(doc.toprettyxml(indent="  "))
f.close()

希望本文所述对大家的Python程序设计有所帮助。

标签:python,xml
0
投稿

猜你喜欢

  • 如何利用python的tkinter实现一个简单的计算器

    2021-09-12 09:19:52
  • python接口测试对修改密码接口进行压测

    2022-05-16 04:26:15
  • hadoop二次排序的原理和实现方法

    2023-01-16 22:42:31
  • Python3数字求和的实例

    2022-05-25 13:37:25
  • Sql Server中的非聚集索引详细介

    2024-01-26 15:57:56
  • 浅谈redux, koa, express 中间件实现对比解析

    2024-05-02 16:28:05
  • 9个Web设计中常见的可用性错误[译]

    2009-03-11 20:25:00
  • python pip如何手动安装二进制包

    2023-07-24 04:09:14
  • mysql调优的几种方式小结

    2024-01-25 22:24:41
  • asp如何向客户端发送提示“出错信息”?

    2010-06-09 18:50:00
  • Zend Framework教程之资源(Resources)用法实例详解

    2023-11-06 02:49:50
  • request基本使用及各种请求方式参数的示例

    2021-03-31 02:32:52
  • Python实现的排列组合计算操作示例

    2022-03-06 19:36:43
  • python实现感知器算法(批处理)

    2022-09-06 18:02:25
  • Python简单实现安全开关文件的两种方式

    2022-09-15 01:54:38
  • Python计算一个给定时间点前一个月和后一个月第一天的方法

    2023-11-26 13:50:26
  • 浅谈Python中range和xrange的区别

    2021-04-18 14:52:13
  • Python基于回溯法子集树模板解决全排列问题示例

    2023-12-18 21:25:04
  • IronPython连接MySQL的方法步骤

    2024-01-27 05:43:05
  • 在VScode中引用自定义模块问题

    2023-08-13 03:50:37
  • asp之家 网络编程 m.aspxhome.com