I can't find a practical solution in the documentation of ElementTree module for avoiding getting "TypeError: cannot serialize None (type NoneType)" when I try to set an attribute to None. Like here:
import xml.etree.ElementTree as ET
myvar = None
p = ET.Element('test')
b = ET.SubElement(p, 'tt')
b.set("last_update",myvar)
tree = ET.ElementTree(p)
tree.write('test.xml')
I know that I could use a series of if myvar is not None: ...
but I would have to repeat the if statement many times. I was wondering if there is a way to avoid writing the attribute at all if the value of the attribute is None.
myvar = "None"
? Attribute values are strings. – Am