I want to generate the following xml file:
<foo if="bar"/>
I've tried this:
from lxml import etree
etree.Element("foo", if="bar")
But I got this error:
page = etree.Element("configuration", if="ok")
^
SyntaxError: invalid syntax
Any ideas?
I'm using python 2.7.9 and lxml 3.4.2
etree.Element("foo", {"if": "bar"})
The attributes can be passed in as a dict:
from lxml import etree
root = etree.Element("foo", {"if": "bar"})
print etree.tostring(root, pretty_print=True)
output
<foo if="bar"/>
etree.Element("foo", **{"if": "bar"})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With