如何访问 child 的 xml 文本?
How to access xml text of child?
我有以下 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>
并且我需要访问 child Attributes
的属性 attribute_name
旁边的文本。
我设法访问了 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}
严格根据问题中的 xml 示例,按照以下方式尝试:
bio = """[your xml sample]"""
doc = ET.fromstring(bio)
attributes = {}
for item in doc.findall('.//Attributes//Attribute'):
attributes[item.attrib['attribute_name']]=item.text
attributes
输出:
{'Individual': 'PK314', 'condition': 'healthy', 'BioSampleModel': 'Generic'}
我有以下 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>
并且我需要访问 child Attributes
的属性 attribute_name
旁边的文本。
我设法访问了 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}
严格根据问题中的 xml 示例,按照以下方式尝试:
bio = """[your xml sample]"""
doc = ET.fromstring(bio)
attributes = {}
for item in doc.findall('.//Attributes//Attribute'):
attributes[item.attrib['attribute_name']]=item.text
attributes
输出:
{'Individual': 'PK314', 'condition': 'healthy', 'BioSampleModel': 'Generic'}