2025-09-10:仓库迁移
This commit is contained in:
22
6.数据编码与处理/6.解析、修改和重写XML.py
Normal file
22
6.数据编码与处理/6.解析、修改和重写XML.py
Normal file
@@ -0,0 +1,22 @@
|
||||
from xml.etree.ElementTree import parse, Element
|
||||
|
||||
if __name__ == '__main__':
|
||||
doc = parse(r'6.数据编码与处理/6.test.xml')
|
||||
# 得到XML的root
|
||||
root = doc.getroot()
|
||||
print(root)
|
||||
|
||||
# 从root里移除一些从root里找到的节点
|
||||
root.remove(root.find('sri'))
|
||||
root.remove(root.find('cr'))
|
||||
|
||||
# 得到某个具体节点的下标
|
||||
print(root.getchildren().index(root.find('nm')))
|
||||
|
||||
# 创建一个节点,并在根节点下插入
|
||||
e = Element('spam')
|
||||
e.text = 'This is a test'
|
||||
root.insert(2, e)
|
||||
|
||||
# 保存为XML文件
|
||||
doc.write(r'6.数据编码与处理/6.test_result.xml', xml_declaration=True)
|
Reference in New Issue
Block a user