Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using "if" as argument identifier

Tags:

python

lxml

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

like image 643
Brenno Martins Avatar asked Jun 21 '26 04:06

Brenno Martins


2 Answers

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"/>
like image 177
Leon Avatar answered Jun 22 '26 17:06

Leon


etree.Element("foo", **{"if": "bar"})
like image 37
Zaaferani Avatar answered Jun 22 '26 18:06

Zaaferani



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!