如何通过Elementtree检查属性存在?

时间:2012-06-21 15:25:14

标签: python xml elementtree

我需要检查存在的某些属性。像:

if "blah-blah-blah" is None:
    print "there is no such attribute"
else:
    print "The attribute exists"

1 个答案:

答案 0 :(得分:16)

Element个对象拥有attrib词典中的所有属性。

if 'blah' not in elem.attrib:
   print "there is no such attribute"