加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

使用lxml将输出格式化为XML

发布时间:2020-12-16 06:03:47 所属栏目:百科 来源:网络整理
导读:我的程序基本上读取了一个输入文件,从该文件生成一个lxml.etree,而不是例如我向etree添加一个节点,然后我想将它打印回一个文件. 所以要把它写回我使用的文件: et.write('DocumentsWrite.xml',pretty_print=True) 我的输出是: Variable Name="one" RefID="
我的程序基本上读取了一个输入文件,从该文件生成一个lxml.etree,而不是例如我向etree添加一个节点,然后我想将它打印回一个文件.
所以要把它写回我使用的文件:
et.write('DocumentsWrite.xml',pretty_print=True)

我的输出是:

<Variable Name="one" RefID="two"><Component Type="three"><Value>four</Value></Component></Variable>

虽然我喜欢这样的东西:

<Variable Name="one" RefID="two">
    <Component Type="three">
        <Value>four</Value>
    </Component> 
</Variable>

我哪里弄错了?我尝试了很多解决方案,但似乎没有工作(beautifulsoup,整洁,解析器…)

这很奇怪,因为它应该是它应该工作的方式.
你能试一试吗:
root = etree.XML( YOUR XML STRING )
print etree.tostring(root,pretty_print=True)

<Variable Name="one" RefID="two">
  <Component Type="three">
    <Value>four</Value>
  </Component>
</Variable>

这应该生成一个格式化的字符串,您可以自己处理.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读