简体   繁体   English

检查python xml.etree.ElementTree模块中的标签是否存在

[英]Check tag existence in python xml.etree.ElementTree module

There is a function of the xml.etree.ElementTree module to do that? xml.etree.ElementTree模块有一个功能可以做到吗? NOT: 不:

if node.find(tag)!=None:
    #code
    pass

That's exactly what you've presented. 这正是您所介绍的。 find() is the tool for the job: find()是完成这项工作的工具:

Finds the first subelement matching match. 查找第一个子元素匹配的匹配项。 match may be a tag name or path. 匹配可以是标签名称或路径。 Returns an element instance or None . 返回一个元素实例或None

It's just that, to follow the best practices, use is when comparing to None : 这只是,遵循最佳实践,使用is比较当None

if node.find(tag) is not None:
    # code

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

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