2021-02-25 21:26:25 发布
网友
我读取一个xml字符串,编辑它,然后想将它再次输出为xml文件。在
但是,当我做.write()时,标题中出现了上述错误
.write()
我发现当您使用ElementTree.fromstring(string)读取xml字符串时,它实际上会创建一个ElementTree.Element,而不是{}本身。元素没有write方法,但ElementTree有。在
ElementTree.fromstring(string)
ElementTree.Element
我的问题是:首先,如何将元素写入xmlfile?或者我如何创建一个ElementTree并将我的元素添加到其中,然后使用.write方法?在
.write
第二,这两种解决方案中的任何一种都会产生什么后果?在
I found out that when you read a xml string using ElementTree.fromstring(string) it will actually create an ElementTree.Element and not a ElementTree itself.
ElementTree
是的,您将获得顶层元素(也称为“document元素”)。在
An Element has no write method but the ElementTree does.
write
^{} constructor signature是这样的:
class xml.etree.ElementTree.ElementTree(element=None, file=None)
因此,这一点非常简单:
编写XML文件时,应始终指定编码。大多数时候,UTF-8是最好的选择。在
是的,您将获得顶层元素(也称为“document元素”)。在
^{} constructor signature 是这样的:
因此,这一点非常简单:
^{pr2}$编写XML文件时,应始终指定编码。大多数时候,UTF-8是最好的选择。在
相关问题
PyPI热门下载资源包