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

前端之家收集整理的这篇文章主要介绍了python – 更紧凑的ElementTree或lxml命名空间前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

@H_301_5@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) )

我也试过这种方法

@H_301_5@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) )

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

@H_301_5@

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

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

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

然后我得到以下输出

@H_301_5@

但我真正想要的是:

@H_301_5@

有任何想法吗?

最佳答案
@H_301_5@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)

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

@H_301_5@

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

@H_301_5@

注意:美化功能here

猜你在找的Python相关文章