我正在使用 ElementTree 库,并且我有一个 xml 标记,该标记具有一个名为“class”的键的属性。现在这显然是 python 中的保留字。任何人都知道如何逃避这个或什至可能吗?
ownerNode = et.SubElement(rootNode, "Owner")
referenceNode = et.SubElement(ownerNode, class="org.identity", name="john")
^^^ 那么,我该如何转义上面的 class 关键字呢?
谢谢!
请您参考如下方法:
基于 the documentation ,看起来您可以在字典中传递属性,键为字符串:
referenceNode = et.SubElement(ownerNode, "refnode", {"class": "org.identity", "name": "john"})
大卫兰伯特,在 python.org thread在讨论这个问题时,指出对于只接受关键字参数的函数 f,您可以这样做:
def f(**kwargs):
print(kwargs)
f(**{'class':'sidebar'})
