简体   繁体   English

Python,如果使用ElementTree解析XML的其他异常

[英]Python if else exception over XML parsed with ElementTree

I have the following XML which I have parsed from a webpage: 我有以下从网页解析的XML:

<!--
Parts from the iGEM Registry of Standard Biological Parts
-->
<rsbpml>
 <part_list>
  <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>SAMT</part_nickname>
   <part_rating/>
   <part_url>http://parts.igem.org/Part:BBa_J45001</part_url>
   <part_entered>2006-06-07</part_entered>
   <part_author>Kate Broadbent</part_author>
   <deep_subparts/>
   <specified_subparts/>
   <specified_subscars/>
   <sequences>...</sequences>
   <features>...</features>
   <parameters>
   <!--...-->
   <!--...-->
    <parameter>...</parameter>
    <parameter>
     <name>swisspro</name>
     <value>Q8H6N2</value>

I have some code to return the swisspro parameter value; 我有一些代码可以返回swisspro参数值; Q8H6N2 . Q8H6N2 However, I want the code to throw up an error if there is no swisspro parameter present. 但是,如果不存在swisspro参数,我希望代码抛出错误。

So far I have tried the code below but it does not work: 到目前为止,我已经尝试了下面的代码,但是它不起作用:

def part_attrib(self,x):
        if x == 'uniprot_id':
            for parameter in self.root.iter(tag='parameter'):
                name = parameter.find('name')
                if name is not None and name.text == 'swisspro':
                    return parameter.find('value').text
                else:
                    return "No UniProt ID present."

With that else statement present, the code always outputs the error statement whether or not there is a swisspro parameter present. 在存在else语句的情况下,无论是否存在swisspro参数,代码都会始终输出错误语句。 If I omit the else argument, the code works but does not throw up an error if there is no swisspro parameter present. 如果我省略else参数,则代码可以工作,但是如果不存在swisspro参数,则不会引发错误。

What am I doing wrong? 我究竟做错了什么?

I should highlight that there are several of these <paramter> sections in the XML. 我应该强调指出,XML中有几个<paramter>部分。

You can set a value before the loop: 您可以在循环之前设置一个值:

swisspro_value = None

for parameter in self.root.iter(tag='parameter'):
    name = parameter.find('name')
    if name is not None and name.text == 'swisspro':
        swisspro_value = parameter.find('value').text
        break

return swisspro_value or "No UniProt ID present."

Since you're returning in your for loop, you could also just return your error message if you make it to the end of the function, 由于您要返回for循环,因此如果返回错误消息到函数末尾,也可以只返回错误消息,

def part_attrib(self,x):
        if x == 'uniprot_id':
            for parameter in self.root.iter(tag='parameter'):
                name = parameter.find('name')
                if name is not None and name.text == 'swisspro':
                    return parameter.find('value').text
        return "No UniProt ID present."

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM