我有以下XML:
<part>
<part_id>151</part_id>
<part_name>BBa_B0034</part_name>
<part_short_name>B0034</part_short_name>
<part_short_desc>RBS (Elowitz 1999) -- defines RBS efficiency</part_short_desc>
<part_type>RBS</part_type>
<release_status>Released HQ 2013</release_status>
<sample_status>In stock</sample_status>
<part_results>Works</part_results>
<part_nickname/>
<part_rating>1</part_rating>
<part_url>http://parts.igem.org/Part:BBa_B0034</part_url>
<part_entered>2003-01-31</part_entered>
我想提取一些值。
例如,我想从BBa_B0034
输出<part_name>
。
我如何使用ElementTree做到这一点?
答案 0 :(得分:0)
正确的解析器需要正确的xml。使用Xml.etree Python进行参考。
参见示例:
>>> import xml.etree.ElementTree as ET
>>> tree = ET.fromstring('<part> <part_id>151</part_id> <part_name>BBa_B0034</part_name> <part_short_name>B0034</part_short_name> <part_short_desc>RBS (Elowitz 1999) -- defines RBSefficiency</part_short_desc> <part_type>RBS</part_type> <release_status>Released HQ 2013</release_status> <sample_status>In stock</sample_status> <part_results>Works</part_results> <part_nickname/> <part_rating>1</part_rating> <part_url>http://parts.igem.org/Part:BBa_B0034</part_url> <part_entered>2003-01-31</part_entered></part>')
>>> tree.findall('./part_name')[0].text
'BBa_B0034'