在Python中使用lxml库编写xml文件

发布于 2024-09-01 16:41:06 字数 411 浏览 4 评论 0原文

我正在使用 lxml 从头开始​​创建 XML 文件; 有这样的代码:

from lxml import etree

root = etree.Element("root")
root.set("interesting", "somewhat")
child1 = etree.SubElement(root, "test")

How do I write root Element object to an x​​ml file using write() method of ElementTree class?

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")

How do I write root Element object to an xml file using write() method of ElementTree class?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

乄_柒ぐ汐 2024-09-08 16:41:07

这是一个简洁的答案,

from lxml import etree

root = etree.Element("root")
root.set("interesting", "somewhat")
child1 = etree.SubElement(root, "test")

my_tree = etree.ElementTree(root)
with open('./filename', 'wb') as f:
    f.write(etree.tostring(my_tree))

您只需将节点放入新树中并将其写入磁盘即可。也适用于 xpath 搜索生成的 HtmlElements

Here's a succinct answer

from lxml import etree

root = etree.Element("root")
root.set("interesting", "somewhat")
child1 = etree.SubElement(root, "test")

my_tree = etree.ElementTree(root)
with open('./filename', 'wb') as f:
    f.write(etree.tostring(my_tree))

you simply place your node in a new tree and write that out to disk. Also works for HtmlElements produced by xpath searches.

小鸟爱天空丶 2024-09-08 16:41:07

这对我有用:

et = etree.ElementTree(document)
with open('sample.xml', 'wb') as f:
    et.write(f, encoding="utf-8", xml_declaration=True, pretty_print=True)

This works for me:

et = etree.ElementTree(document)
with open('sample.xml', 'wb') as f:
    et.write(f, encoding="utf-8", xml_declaration=True, pretty_print=True)
尛丟丟 2024-09-08 16:41:07

您可以将文件名赋予 ElementTree 的 write()

etree.ElementTree(root).write('output.xml')

You can give the filename to the write() of ElementTree

etree.ElementTree(root).write('output.xml')
挽你眉间 2024-09-08 16:41:06

您可以从元素中获取一个字符串,然后从 lxml 教程 中编写该字符串,

str = etree.tostring(root, pretty_print=True)

查看 tostring 文档以设置编码 - 这是用 Python 2 编写的,Python 3 给出了一个二进制字符串,可以直接写入文件,但可能不是您想要的代码。

或转换为元素树(最初写入文件句柄,但在我编写此文件时错过了,或者它是新的,它可以是文件名,如

et = etree.ElementTree(root)
et.write('output.xml', pretty_print=True)

You can get a string from the element and then write that from lxml tutorial

str = etree.tostring(root, pretty_print=True)

Look at the tostring documentation to set the encoding - this was written in Python 2, Python 3 gives a binary string back which can be written directly to file but is probably not what you want in code.

or convert to an element tree (originally write to a file handle but either missed when I wrote this or it is new it can be a file name as per this answer )

et = etree.ElementTree(root)
et.write('output.xml', pretty_print=True)
我的鱼塘能养鲲 2024-09-08 16:41:06

您可以尝试下面的代码。

from lxml import etree as ET

root = ET.Element('Doc')
level1 = ET.SubElement(root, 'S')
main = ET.SubElement(level1, 'Text')
main.text = 'Thanks for contributing an answer to Stack Overflow!'
second = ET.SubElement(level1, 'Tokens')
level2 = ET.SubElement(second, 'Token', word=u"low")


level3 = ET.SubElement(level2, 'Morph')
second1 = ET.SubElement(level3, 'Lemma')
second1.text = 'sdfs'
second1 = ET.SubElement(level3, 'info')
second1.text = 'qw'

level4 = ET.SubElement(level3, 'Aff')
second1 = ET.SubElement(level4, 'Type')
second1.text = 'sdfs'
second1 = ET.SubElement(level4, 'Suf')
second1.text = 'qw'

tree = ET.ElementTree(root)
tree.write('output.xml', pretty_print=True, xml_declaration=True,   encoding="utf-8")

You can try the below code.

from lxml import etree as ET

root = ET.Element('Doc')
level1 = ET.SubElement(root, 'S')
main = ET.SubElement(level1, 'Text')
main.text = 'Thanks for contributing an answer to Stack Overflow!'
second = ET.SubElement(level1, 'Tokens')
level2 = ET.SubElement(second, 'Token', word=u"low")


level3 = ET.SubElement(level2, 'Morph')
second1 = ET.SubElement(level3, 'Lemma')
second1.text = 'sdfs'
second1 = ET.SubElement(level3, 'info')
second1.text = 'qw'

level4 = ET.SubElement(level3, 'Aff')
second1 = ET.SubElement(level4, 'Type')
second1.text = 'sdfs'
second1 = ET.SubElement(level4, 'Suf')
second1.text = 'qw'

tree = ET.ElementTree(root)
tree.write('output.xml', pretty_print=True, xml_declaration=True,   encoding="utf-8")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文