在XML元素之间添加行:Python

2024-04-24 01:17:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在创建XML元素并对其进行预打印,如图所示:

rootElement = Element("root)
childElement = SubElement(rootElement, "child")
def prettify(elem):
    rough_string = ElementTree.tostring(elem, 'utf-8')
    reparsed = minidom.parseString(rough_string)
    return reparsed.toprettyxml(indent="  ")
print prettify(rootElement)

输出看起来像

<?xml version="1.0" ?>
<root>
  <child/>
</root>

如何在这些元素之间添加其他行?你知道吗

<?xml version="1.0" ?>
<root>
 <!-- how to add line spaces here -->
  <child/>
</root>

Tags: child元素stringversionrootxmlelementprettify
1条回答
网友
1楼 · 发布于 2024-04-24 01:17:03

官方文件说

Node.toprettyxml(indent="\t", newl="\n", encoding=None)

newl参数处理行距。所以你可以给一个或多个行空间来做这个。你知道吗

相关问题 更多 >