0

我需要强制 python(2.7.5) 在构建 xml 文件时使用单词类

    properties = ET.SubElement(head, "properties", class="model.View$PropertyList")
                                                       ^
SyntaxError: invalid syntax

我试过''或“”

    properties = ET.SubElement(head, "properties", "class"="hudson.model.View$PropertyList")
SyntaxError: keyword can't be an expression

如果我将其更改为另一个名称(foo),它会构建 xml:

<properties foo="hudson.model.View$PropertyList" />
4

1 回答 1

1

您可以使用attrib={}语法:

head = ET.Element('head')

properties = ET.SubElement(head, "properties", attrib={'class':"model.View$PropertyList"})

ET.tostring(head)
'<head><properties class="model.View$PropertyList" /></head>'
于 2014-11-05T18:52:55.213 回答