Search code examples
pythonxmlstack-overflowdata-sciencekeyerror

How to check if an Attribute is contained in XML?


I am getting a KeyError: 'About Me'. I have figured out that the dump Data of Stackoverflow for Users contain some User that doesn't have an AboutMe Variable. The iteration through the xml['AboutMe'] works fine. But If some User has not an AboutMe Variable, the script crashes and give me an KeyError.

I try to check if AboutMe is contained. If it is it should print the AboutMe without HTML Tag out.

If it is not contained. It should assign the value None to this entry.

That is my Code:

 if element['AboutMe'] is not None:
    cleanAboutMe = BeautifulSoup(element['AboutMe'], "lxml").text
    print(cleanAboutMe)
else:
    element = {'AboutMe':'None'}

The output that I get:

This is a puppet test account I use to validate "regular user" stuff on the site
-- Jeff Atwood
Independent software engineer

I'm not takin' my sneakers off!
if element['AboutMe'] is not None:
KeyError: 'AboutMe'

I hope you guys can help. I search and try every asked Question in Stackoverflow but None helped me. I get everytime this Key Error.

A little Note: The variable Element does work fine but if I pointing to the AboutMe Key then I get the Error.

Best Regards

Theeninfam


Solution

  • If the element might not have that attribute, use element.get('AboutMe') to get the attribute value or None if the attribute is not present. get is a standard function for ElementTree implementations. https://docs.python.org/3/library/xml.etree.elementtree.html#xml.etree.ElementTree.Element.get