1

I'm using ElementTree to successfully pull values from specific tags in an xml file. 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. 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.

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.

1 Answer 1

5

"None" (a string) is not the same as None (a constant representing the absense of a value).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.