删除"xmlns:py ..."与lxml.objectify [英] Remove "xmlns:py..." with lxml.objectify

查看:57
本文介绍了删除"xmlns:py ..."与lxml.objectify的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚发现了lxml.objectify,它看起来很容易阅读/编写简单的XML文件.

I just discovered lxml.objectify which seems nice and easy for reading/writing simple XML files.

首先,使用lxml.objectify是个好主意吗?例如,它是否已经成熟并且仍在开发中,并且有可能在将来推出?

Firstly, is it a good idea to use lxml.objectify? For instance is it mature and still developed and likely to be available in the future?

第二,如何防止objectify在下面的输出中添加类似xmlns:py="http://codespeak.net/lxml/objectify/pytype" py:pytype="str"的标记?

Secondly, how do I prevent objectify from addding markup like xmlns:py="http://codespeak.net/lxml/objectify/pytype" py:pytype="str" in the output below ?.

输入:config.xml

<?xml version="1.0" encoding="utf-8"?>
<Test>
  <MyElement1>sdfsdfdsfd</MyElement1>
</Test>


代码

from lxml import etree, objectify

with open('config.xml') as f:
    xml = f.read()
root = objectify.fromstring(xml)

root.Information = 'maybe'

print etree.tostring(root, pretty_print=True)


输出

<Test>
  <MyElement1>sdfsdfdsfd</MyElement1>
  <Information xmlns:py="http://codespeak.net/lxml/objectify/pytype" py:pytype="str">maybe</Information>
</Test>

推荐答案

此处指出:

As pointed out here : When using lxml, can the XML be rendered without namespace attributes?, this is enough to prevent this xmlns markup to appear :

objectify.deannotate(root, xsi_nil=True)
etree.cleanup_namespaces(root)

这篇关于删除"xmlns:py ..."与lxml.objectify的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆