0
votes

I have an XML file that I'm reading and modifying using xml.etree.ElementTree in python 2.6 and 2.7. The ElementTree module is automatically changing anything with a value of "true" or "false" (lowercase) to a boolean, and then writing it to the modified file as "True" or "False".

How can I write these keys as str(True) and str(False) so they stay in lower case?

1

1 Answers

1
votes

Use: str(True).lower():

>>> from lxml import etree
>>> root = etree.Element("root")
>>> tag = etree.SubElement(root, 'tag')
>>> tag.set("booleanValue", str(True).lower())
>>> etree.tostring(root, xml_declaration=True, encoding="utf-8")
'<?xml version=\'1.0\' encoding=\'utf-8\'?>\n<root><tag booleanValue="true"/></root>'