栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

lxml:将名称空间添加到输入文件

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

lxml:将名称空间添加到输入文件

在lxml中无法修改节点的名称空间映射。请将此开放票证将此功能作为愿望清单项目。

它起源于lxml邮件列表上的该线程,其中提供了一种替代方法来替换根节点。但是,替换根节点存在一些问题:请参见上面的票证。

为了完整性,我将建议的根替换解决方法代码放在此处:

>>> DOC = """<sbml xmlns="http://www.sbml.org/sbml/level2/version4" xmlns:celldesigner="http://www.sbml.org/2001/ns/celldesigner" level="2" version="4">...   <model metaid="untitled" id="untitled">...     <annotation>...</annotation>...     <listOfUnitDefinitions>...</listOfUnitDefinitions>...     <listOfCompartments>...</listOfCompartments>...     <listOfSpecies>...       <species metaid="s1" id="s1" name="GenA" compartment="default" initialAmount="0">...         <annotation>...<celldesigner:extension>...</celldesigner:extension>...         </annotation>...       </species>...       <species metaid="s2" id="s2" name="s2" compartment="default" initialAmount="0">...         <annotation>... <celldesigner:extension>...</celldesigner:extension>...         </annotation>...       </species>...     </listOfSpecies>...     <listOfReactions>...</listOfReactions>...   </model>... </sbml>""">>> >>> from lxml import etree>>> from StringIO import StringIO>>> NS = "http://this.is.some/custom_namespace">>> tree = etree.ElementTree(element=None, file=StringIO(DOC))>>> root = tree.getroot()>>> nsmap = root.nsmap>>> nsmap['kjw'] = NS>>> new_root = etree.Element(root.tag, nsmap=nsmap)>>> new_root[:] = root[:]>>> new_root.append(etree.Element('{%s}%s' % (NS, 'test')))>>> new_root.append(etree.Element('{%s}%s' % (NS, 'test')))>>> print etree.tostring(new_root, pretty_print=True)<sbml xmlns:celldesigner="http://www.sbml.org/2001/ns/celldesigner" xmlns:kjw="http://this.is.some/custom_namespace" xmlns="http://www.sbml.org/sbml/level2/version4"><model metaid="untitled" id="untitled">    <annotation>...</annotation>    <listOfUnitDefinitions>...</listOfUnitDefinitions>    <listOfCompartments>...</listOfCompartments>    <listOfSpecies>      <species metaid="s1" id="s1" name="GenA" compartment="default" initialAmount="0">        <annotation>          <celldesigner:extension>...</celldesigner:extension>        </annotation>      </species>      <species metaid="s2" id="s2" name="s2" compartment="default" initialAmount="0">        <annotation><celldesigner:extension>...</celldesigner:extension>        </annotation>      </species>    </listOfSpecies>    <listOfReactions>...</listOfReactions>  </model><kjw:test/><kjw:test/></sbml>


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/633804.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号