首页 > 解决方案 > ElementTree 漂亮打印空行

问题描述

我正在使用下面的代码行在 python 2.7 中漂亮地打印 elementtree 对象:

xml_str = minidom.parseString(ET.tostring(tgt_xml.getroot())).toprettyxml(indent="\t", encoding='utf-8')
with open(final_path, "w") as f:
        f.write(xml_str)

它完成了它的工作,但在每行 xml 之后添加了 2 个空白行......所以结果看起来像

<Workflow name="Some_name">


    <taskWorkflow>


        <taskWorkflow retainSysIds="false">


            <variables>


                <variable>


                    <description/>

有人知道如何在没有空行的情况下打印 elementtree 吗?

标签: pythonpython-2.7elementtreepretty-printminidom

解决方案


推荐阅读