etree SubElement属性名称类失败 [英] etree SubElement attribute name class fails

查看:101
本文介绍了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属性名称类失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆