首页 > 文章列表 > python中XML删除元素

python中XML删除元素

Python XML
401 2022-08-07

1、使用pop()方法,将删除用户不需要的属性或元素。

通过下标值实现功能,默认情况下清除列表中的最后一个元素,每次只能清除一个元素。

myroot[0][0].attrib.pop('name', None)
# create a new XML file with the results
mytree.write('output5.xml')

2、如果想删除所有标签,可以使用clear函数,移除列表中的所有元素。

等价于del a[:]。

myroot[0].clear()
mytree.write('output7.xml')

本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。