本文介绍了etree SubElement属性名称类失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要强制python(2.7.5)在构建xml文件时使用单词类
I need to force python(2.7.5) to use the word class in building a xml file
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:
If I change it to another name (foo), it builds the xml:
<properties foo="hudson.model.View$PropertyList" />
推荐答案
您可以使用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>'
这篇关于etree SubElement属性名称类失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!