首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >删除“xmlns:py.”用lxml.objectify

删除“xmlns:py.”用lxml.objectify
EN

Stack Overflow用户
提问于 2014-02-13 22:52:17
回答 1查看 1.4K关注 0票数 2

我刚刚发现了lxml.objectify,它看起来很好读/写简单的XML文件。

首先,使用lxml.objectify是个好主意吗?例如,它是否已经成熟,是否仍在开发,并有可能在今后提供?

其次,如何防止objectify在下面的输出中添加像xmlns:py="http://codespeak.net/lxml/objectify/pytype" py:pytype="str"这样的标记?

输入: config.xml

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<Test>
  <MyElement1>sdfsdfdsfd</MyElement1>
</Test>

代码语言:javascript
复制
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)

输出

代码语言:javascript
复制
<Test>
  <MyElement1>sdfsdfdsfd</MyElement1>
  <Information xmlns:py="http://codespeak.net/lxml/objectify/pytype" py:pytype="str">maybe</Information>
</Test>
EN

回答 1

Stack Overflow用户

发布于 2014-02-14 09:51:54

正如这里指出的:When using lxml, can the XML be rendered without namespace attributes?,这足以防止这个xmlns标记出现:

代码语言:javascript
复制
objectify.deannotate(root, xsi_nil=True)
etree.cleanup_namespaces(root)
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21767300

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档