我已经尝试查看与此类似的其他问题,但似乎没有人回答 xml.etree.ElementTree。
目前我的代码看起来像这样(它只是一个简单的 XML 生成器)
import xml.etree.ElementTree as ET
example1=ET.Element('example1')
example2=ET.SubElement(example1, 'example2').text='1234'
tree = ET.ElementTree(example1)
NewXML='example.xml'
tree.write(NewXML,encoding = 'UTF-8', xml_declaration = True)
目前的输出只是这个文件:
<?xml version='1.0' encoding='UTF-8'?>
<example1>
<example2>1234</example2>
</example1>
我想添加声明 Standalone = 'yes' 所以输出应该是:
<?xml version='1.0' encoding='UTF-8' standalone = 'yes'?>
<example1>
<example2>1234</example2>
</example1>
然而,这就是我遇到问题的地方。
我试过了
tree.write(NewXML,encoding = "UTF-8", xml_declaration = True, standalone = True)
tree.write(NewXML,encoding = "UTF-8", xml_declaration = True, standalone = "yes")
但我收到此错误:TypeError: write() got an unexpected keyword argument 'standalone'