在 Stackoverflow 的几个人的帮助下,我想出了以下代码:
import lxml.etree
f = open("PaVE.tre.xml", "r")
out = open("PaVE.2.xml", "a")
data = f.read()
line=["HPV16","Alpha"]
tree = lxml.etree.XML(data)
nsmap = {'phylo': 'http://www.phyloxml.org'}
matches = tree.xpath('//phylo:name[text()="HPV16"]', namespaces=nsmap)
for e in matches:
#do something fun
但是,我将 HPV16 硬编码到 xpath 表达式中。我想从线 [0] 中取出 HPV16。我在想类似的事情:
matches = tree.xpath('//phylo:name[text()='+line[0]+']', namespaces=nsmap)
但是,这似乎不起作用!一如既往,任何帮助将不胜感激
编辑:我按照要求从 xml 文件中添加了几行:
<phyloxml xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.phyloxml.org" xsi:schemaLocation="http://www.phyloxml.org http://www.phyloxml.org/1.10/phyloxml.xsd">
<phylogeny rooted="true">
<clade>
<clade>
<branch_length>0.5</branch_length>
<clade>
<name>HPV16</name>
<branch_length>1.0</branch_length>
</clade>
<clade>