使用 Python 仅将 xml 声明写入文件
Writing just the xml declaration to file using Python
我正在通过遍历字符串列表在 python 中编写多个 xml 文件。
假设我有:
from xml.etree.ElementTree import ElementTree, Element, SubElement, tostring
parent = Element('parent')
child = SubElement(parent, 'child')
f = open('file.xml', 'w')
document = ElementTree(parent)
l = ['a', 'b', 'c']
for ch in l:
child.text = ch
document.write(f, encoding='utf-8', xml_declaration=True)
输出:
<?xml version='1.0' encoding='utf-8'?>
<parent><child>a</child></parent><?xml version='1.0' encoding='utf-8'?>
<parent><child>b</child></parent><?xml version='1.0' encoding='utf-8'?>
<parent><child>c</child></parent>
期望的输出:
<?xml version='1.0' encoding='utf-8'?>
<parent>
<child>a</child>
<child>b</child>
<child>c</child>
</parent>
我只希望 xml 声明在文件顶部出现一次。可能应该在循环之前将声明写入文件,但是当我尝试这样做时,我得到了空元素。我不想这样做:
f.write('<?xml version='1.0' encoding='utf-8'?>')
如何将 xml 声明写入文件?
编辑:期望的输出
这在技术上是可行的,只需在 xml_declaration
:
上设置 bool() 标志
parent = Element('parent')
child = SubElement(parent, 'child')
f = open('file.xml', 'w')
document = ElementTree(parent)
l = ['a', 'b', 'c']
# use enumerate to have (index, element) pair, started from 0
for i, ch in enumerate(l):
child.text = ch
# Start index=0, since bool(0) is Fale, and bool(1..n) is True
# the flag will be offset
document.write(f, encoding='utf-8', xml_declaration=bool(not i))
f.close()
已更新:
由于OP已经意识到想要的输出在语法上不正确,并且改变了要求,这里是正常的处理方式xml:
from xml.etree.ElementTree import ElementTree, Element, SubElement, tostring
parent = Element('parent')
f = open('file.xml', 'w')
document = ElementTree(parent)
l = ['a', 'b', 'c']
for ch in l:
child = SubElement(parent, 'child')
child.text = ch
document.write(f, encoding='utf-8', xml_declaration=True)
f.close()
我不熟悉 Python 的 XML 库,但让我们退后一步。如果你做你想做的,你的输出将是无效的 XML。 XML 必须恰好有一个 root element.
所以,你可以:
<?xml version='1.0' encoding='utf-8'?>
<uberparent>
<parent><child>a</child></parent>
<parent><child>b</child></parent>
<parent><child>c</child></parent>
</uberparent>
假设您正在创建一个 Google 站点地图,例如:their schema 表示根元素是 "urlset"。
您需要在写入文件之前将子项添加到树中。在您的代码中,您覆盖了相同的元素并在循环的每次迭代中编写了整个 XML 文档。
此外,您的 XML 语法缺少有效的顶级 "root" 元素。
from xml.etree.ElementTree import ElementTree, Element, SubElement, tostring
root = Element('root')
l = ['a', 'b', 'c']
for ch in l:
parent = SubElement(root,'parent')
child = SubElement(parent, 'child')
child.text = ch
document = ElementTree(root)
document.write('file.xml', encoding='utf-8', xml_declaration=True)
输出将是:
<?xml version='1.0' encoding='utf-8'?>
<root>
<parent><child>a</child></parent>
<parent><child>b</child></parent>
<parent><child>c</child></parent>
</root>
我正在通过遍历字符串列表在 python 中编写多个 xml 文件。 假设我有:
from xml.etree.ElementTree import ElementTree, Element, SubElement, tostring
parent = Element('parent')
child = SubElement(parent, 'child')
f = open('file.xml', 'w')
document = ElementTree(parent)
l = ['a', 'b', 'c']
for ch in l:
child.text = ch
document.write(f, encoding='utf-8', xml_declaration=True)
输出:
<?xml version='1.0' encoding='utf-8'?>
<parent><child>a</child></parent><?xml version='1.0' encoding='utf-8'?>
<parent><child>b</child></parent><?xml version='1.0' encoding='utf-8'?>
<parent><child>c</child></parent>
期望的输出:
<?xml version='1.0' encoding='utf-8'?>
<parent>
<child>a</child>
<child>b</child>
<child>c</child>
</parent>
我只希望 xml 声明在文件顶部出现一次。可能应该在循环之前将声明写入文件,但是当我尝试这样做时,我得到了空元素。我不想这样做:
f.write('<?xml version='1.0' encoding='utf-8'?>')
如何将 xml 声明写入文件?
编辑:期望的输出
这在技术上是可行的,只需在 xml_declaration
:
parent = Element('parent')
child = SubElement(parent, 'child')
f = open('file.xml', 'w')
document = ElementTree(parent)
l = ['a', 'b', 'c']
# use enumerate to have (index, element) pair, started from 0
for i, ch in enumerate(l):
child.text = ch
# Start index=0, since bool(0) is Fale, and bool(1..n) is True
# the flag will be offset
document.write(f, encoding='utf-8', xml_declaration=bool(not i))
f.close()
已更新:
由于OP已经意识到想要的输出在语法上不正确,并且改变了要求,这里是正常的处理方式xml:
from xml.etree.ElementTree import ElementTree, Element, SubElement, tostring
parent = Element('parent')
f = open('file.xml', 'w')
document = ElementTree(parent)
l = ['a', 'b', 'c']
for ch in l:
child = SubElement(parent, 'child')
child.text = ch
document.write(f, encoding='utf-8', xml_declaration=True)
f.close()
我不熟悉 Python 的 XML 库,但让我们退后一步。如果你做你想做的,你的输出将是无效的 XML。 XML 必须恰好有一个 root element.
所以,你可以:
<?xml version='1.0' encoding='utf-8'?>
<uberparent>
<parent><child>a</child></parent>
<parent><child>b</child></parent>
<parent><child>c</child></parent>
</uberparent>
假设您正在创建一个 Google 站点地图,例如:their schema 表示根元素是 "urlset"。
您需要在写入文件之前将子项添加到树中。在您的代码中,您覆盖了相同的元素并在循环的每次迭代中编写了整个 XML 文档。
此外,您的 XML 语法缺少有效的顶级 "root" 元素。
from xml.etree.ElementTree import ElementTree, Element, SubElement, tostring
root = Element('root')
l = ['a', 'b', 'c']
for ch in l:
parent = SubElement(root,'parent')
child = SubElement(parent, 'child')
child.text = ch
document = ElementTree(root)
document.write('file.xml', encoding='utf-8', xml_declaration=True)
输出将是:
<?xml version='1.0' encoding='utf-8'?>
<root>
<parent><child>a</child></parent>
<parent><child>b</child></parent>
<parent><child>c</child></parent>
</root>