Since you are interested only in the first element of findall
, you can replace findall(x)[0]
with find(x)
. Besides, if you want to avoid try/except blocks, you can use ternary.
departedmembers = doc_root.findall('{http://www.ordnancesurvey.co.uk/xml/namespaces/osgb}departedMember')
for departedMember in departedMembers:
...
date = departedmember[0].find('{http://www.ordnancesurvey.co.uk/xml/namespaces/osgb}deletionDate')
date = None if date == None else date.text # Considering you want to set the element to None if it was not found