python写入xml文件的方法

yipeiwu_com6年前Python基础

本文实例讲述了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面向对象类继承和组合实例分析

本文实例讲述了Python面向对象类继承和组合。分享给大家供大家参考,具体如下: 在python3中所有类默认继承object,凡是继承了object的类都成为新式类,以及该子类的子类P...

pyqt5 使用label控件实时显示时间的实例

pyqt5 使用label控件实时显示时间的实例

如下所示: import sys from PyQt5 import QtGui, QtCore, QtWidgets from PyQt5.QtWidgets import * f...

python用插值法绘制平滑曲线

python用插值法绘制平滑曲线

本文实例为大家分享了python用插值法绘制平滑曲线的具体代码,供大家参考,具体内容如下 原图: 平滑处理后: 代码实现如下: # 1. 随机构造数据 import numpy...

利用Pandas 创建空的DataFrame方法

平时写pyhton的时候习惯初始化一些list啊,tuple啊,dict啊这样的。一用到Pandas的DataFrame数据结构也就总想着初始化一个空的DataFrame,虽然没什么太大...

Python version 2.7 required, which was not found in the registry

Python version 2.7 required, which was not found in the registry

安装PIL库的时候,直接提示:Python version 2.7 required, which was not found in the registry。 如图: 大意是说找不到...