如何删除 etree 元素的属性?
我的 etree 元素具有一些属性 - 我们如何删除特定 etree 元素的属性。
I've Element of etree having some attributes - how can we delete the attribute of perticular etree Element.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
元素对象的
.attrib
成员 包含以下字典属性 - 您可以使用.pop("key")
或del
就像在任何其他字典上一样 删除键值对。The
.attrib
member of the element object contains the dict of attributes - you can use.pop("key")
ordel
like you would on any other dict to remove a key-val pair.当您弹出不可用的键时,您不需要
try/ except
。以下是您可以执行此操作的方法。代码
You do not need to
try/except
while you are popping a key which is unavailable. Here is how you can do this.Code
示例:
有时要小心您没有该属性:
始终建议我们处理异常。
Example :
Take care sometimes you dont have the attribute:
It is always suggested that we handle exceptions.