带符号的属性名称 XML with Python 3.6.2

Attribute name with symbols in XML with Python 3.6.2

我正在使用 ElemenTree 创建 XML 文件。在节点的接收服务器属性的规范中应该是 "name-1",但是如果我尝试使用它 IDE 会收到一个错误。请建议此属性的正确语法。

import xml.etree.cElementTree as ET

Query = ET.Element("Query")
Command = ET.SubElement(Query, "Command)
Items = ET.SubElement(Command, "Items")
ET.SubElement(Items, "field2", name-1="Name").text

使用字典创建属性。这可以防止 name-1 被解释为“name 减一”。

ET.SubElement(Items, "field2", {"name-1": "Name"})