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

python – 更紧凑的ElementTree或lxml命名空间

发布时间:2020-12-16 21:54:42 所属栏目:Python 来源:网络整理
导读:当子元素作为父元素位于不同的命名空间时,我试图在ElementTree或lxml中获得命名空间的紧凑表示.这是基本的例子: from lxml import etreecountry = etree.Element("country")name = etree.SubElement(country,"{urn:test}name")name.text = "Canada"populati

当子元素作为父元素位于不同的命名空间时,我试图在ElementTree或lxml中获得命名空间的紧凑表示.这是基本的例子:

from lxml import etree

country = etree.Element("country")

name = etree.SubElement(country,"{urn:test}name")
name.text = "Canada"
population = etree.SubElement(country,"{urn:test}population")
population.text = "34M"
etree.register_namespace('tst','urn:test')

print( etree.tostring(country,pretty_print=True) )

我也试过这种方法:

ns = {"test" : "urn:test"}

country = etree.Element("country",nsmap=ns)

name = etree.SubElement(country,"{test}name")
name.text = "Canada"
population = etree.SubElement(country,"{test}population")
population.text = "34M"

print( etree.tostring(country,pretty_print=True) )

在这两种情况下,我得到这样的东西:

虽然这是正确的,但我希望它不那么冗长 – 这可能成为大数据集的真正问题(特别是因为我使用比’urn:test’更大的NS).

如果我可以将’country’放在“urn:test”命名空间内,并像这样声明它(在上面的第一个例子中):

country = etree.Element("{test}country")

然后我得到以下输出:

但我真正想要的是:

有任何想法吗?

最佳答案
from xml.etree import cElementTree as ET
##ET.register_namespace('tst','urn:test')
country = ET.Element("country")
name = ET.SubElement(country,"{urn:test}name")
name.text = "Canada"
population = ET.SubElement(country,"{urn:test}population")
population.text = "34M"
print prettify(country)

上面将给出(没有注册任何命名空间):

并且,当我删除评论的部分时,它将给出::

注意:美化功能是here

(编辑:李大同)

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

    推荐文章
      热点阅读