简体   繁体   English

找不到标签的python xml ElementTree异常

[英]python xml ElementTree exception for tag not found

I'm using ElementTree to successfully pull values from specific tags in an xml file. 我正在使用ElementTree从xml文件中的特定标签成功提取值。 To add some level of protection I'd like to throw and exception if a required tag is not present. 为了增加某种程度的保护,如果不存在必需的标签,我想抛出异常。

I found that ElementTree returns a value of "None" in place of a missing tag. 我发现ElementTree返回值“ None”代替丢失的标签。 But I can't even get that to work to my advantage :( 但是我什至无法使它发挥我的优势:(

value = xmlTree.findtext('tag')
print value

Will print 'None" if the tag "tag" isn't present. 如果标签“ tag”不存在,将打印“ None”。

So I tried 所以我尝试了

if value == "None":
    print "tag not present"

But for some reason it won't catch it??? 但是由于某种原因,它将无法捕获???

My next thought was to see if ElementTree had a build in exception that would detect a missing tag, but had no luck with that either. 我的下一个想法是查看ElementTree是否具有异常构建,该异常可以检测丢失的标签,但也没有运气。

"None" (字符串)None (表示值缺失的常量)不同

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

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