在Python中使用lxml库编写xml文件 [英] Write xml file using lxml library in Python
本文介绍了在Python中使用lxml库编写xml文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用 lxml 从头开始创建XML文件; 像这样的代码:
I'm using lxml to create an XML file from scratch; having a code like this:
from lxml import etree
root = etree.Element("root")
root.set("interesting", "somewhat")
child1 = etree.SubElement(root, "test")
如何使用ElementTree
类的write()
方法将根Element
对象写入xml文件?
How do I write root Element
object to an xml file using write()
method of ElementTree
class?
推荐答案
You can get a string from the element and then write that from lxml tutorial
str = etree.tostring(root, pretty_print=True)
或转换为元素树
et = etree.ElementTree(root)
et.write(sys.stdout, pretty_print=True)
这篇关于在Python中使用lxml库编写xml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文