【发布时间】:2020-08-05 10:33:38
【问题描述】:
我有以下 xml 文件(取自 here:
<BioSampleSet>
<BioSample submission_date="2011-12-01T13:31:02.367" last_update="2014-11-08T01:40:24.717" publication_date="2012-02-16T10:49:52.970" access="public" id="761094" accession="SAMN00761094">
<Ids>
</Ids>
<Package display_name="Generic">Generic.1.0</Package>
<Attributes>
<Attribute attribute_name="Individual">PK314</Attribute>
<Attribute attribute_name="condition">healthy</Attribute>
<Attribute attribute_name="BioSampleModel">Generic</Attribute>
</Attributes>
<Status status="live" when="2014-11-08T00:27:24"/>
</BioSample>
</BioSampleSet>
而且我需要访问子属性attribute_name 旁边的文本Attributes。
我设法访问了 attribute_name.:
from Bio import Entrez,SeqIO
Entrez.email = '#'
import xml.etree.ElementTree as ET
handle = Entrez.efetch(db="biosample", id="SAMN00761094", retmode="xml", rettype="full")
tree = ET.parse(handle)
for attr in root[0].iter('Attribute'):
name = attr.get('attribute_name')
print(name)
打印出来:
Individual
condition
BioSampleModel
如何创建attribute_name 的值的dict 及其旁边的文本?
我想要的输出是
attributes = {'Individual': PK314, 'condition': healthy, 'BioSampleModel': Generic}
【问题讨论】:
标签: python-3.x xml elementtree ncbi