使用 "if" 作为参数标识符
Using "if" as argument identifier
我想生成以下 xml 文件:
<foo if="bar"/>
我试过这个:
from lxml import etree
etree.Element("foo", if="bar")
但是我得到了这个错误:
page = etree.Element("configuration", if="ok")
^
SyntaxError: invalid syntax
有什么想法吗?
我正在使用 python 2.7.9 和 lxml 3.4.2
'if'是Python中的保留字,也就是说不能作为标识符使用。
etree.Element("foo", {"if": "bar"})
属性可以作为字典传入:
from lxml import etree
root = etree.Element("foo", {"if": "bar"})
print etree.tostring(root, pretty_print=True)
输出
<foo if="bar"/>
etree.Element("foo", **{"if": "bar"})
我想生成以下 xml 文件:
<foo if="bar"/>
我试过这个:
from lxml import etree
etree.Element("foo", if="bar")
但是我得到了这个错误:
page = etree.Element("configuration", if="ok")
^
SyntaxError: invalid syntax
有什么想法吗?
我正在使用 python 2.7.9 和 lxml 3.4.2
'if'是Python中的保留字,也就是说不能作为标识符使用。
etree.Element("foo", {"if": "bar"})
属性可以作为字典传入:
from lxml import etree
root = etree.Element("foo", {"if": "bar"})
print etree.tostring(root, pretty_print=True)
输出
<foo if="bar"/>
etree.Element("foo", **{"if": "bar"})