ElementTree模块生成、解析xml
⼀,XML⽂件格式介绍
<tag attrib = > text </tag> tail
例:<APP_KEY channel = 'CSDN'> hello123456789 </APP_KEY>
女生最好就业的专业
- tag,即标签,⽤于标识该元素表⽰哪种数据,即APP_KEY
- attrib,即属性,⽤Dictionary形式保存,即{‘channel’ = ‘CSDN’}
- text,⽂本字符串,可以⽤来存储⼀些数据,即hello123456789
- tail,尾字符串,并不是必须的。
⼆,⽰例
'''
在Python标准库中,ElementTree有两种实现⽅式:⼀种是纯Python的实现ElementTree,另
⼀种是速度更快⼀点的c ElementTree。如果不确定环境中是否有cElementTree,可以使⽤如下的⽅式导⼊
但从Python 3.3开始,会默认使⽤cElementTree来加快速度,但是之前的版本最好使⽤如上的代码,以提⾼代码的兼容性。
'''
try:
cElementTree as ET
except ImportError:
ElementTree as ET
def makeXML():
# 创建⼀个根节点,标签名为Root
root_element = ET.Element("Root")
# 插⼊两个⼦节点
sub_element1 = ET.SubElement(root_element, "sub_elemtnt1")
sub_element2 = ET.SubElement(root_element, "sub_elemtnt2")
for i in range(5):
# 设置tag 以及
subb = ET.SubElement(sub_element1, f"name_{i}", tag=f"tag_{i}", attrib={
"name": f"value_{i}"})
# 为标签赋值
< = f"abc{i}"
subb2 = ET.SubElement(sub_element2, f"name_{i}", tag=f"tag_{i}", attrib={
"name": f"value_{i}"})
< = f"efg{i}"
# 创建xml树,并将根节点放⼊其中
xml_tree = ET.ElementTree(root_element)
# 将xml 写⼊到⽂件中,参数1:xml⽂件⽣成的位置和名字,参数2:指定xml编码,参数3:xml 宣⾔,
# 即:是否有 <?xml version='1.0' encoding='utf-8'?>  (这个⽅法⽣成的xml⽂件使⽤⽂本打开格式是没有缩进的)
xml_tree.write(r"C:\Users\v-yunhgu\l",
encoding="utf-8", xml_declaration=True)
# 第⼆种写⼊⽂件的⽅法(这个⽅法⽣成的xml⽂件使⽤⽂本打开格式是有缩进的)
# ET模块转换root为bytes输出
xml_string = ET.tostring(root_element)
from xml.dom import minidom
dom = minidom.parseString(xml_string)
with open(r"C:\Users\v-yunhgu\l", 'w', encoding='utf-8') as f:
# indent为根节点缩进,newl每⾏数据句末符号,addindent为其他节点缩进
dom.writexml(f, indent='\t', newl='\n',
addindent='\t', encoding='utf-8')
北京好玩的地方有哪些def getXML():
xmlPath = r"C:\Users\v-yunhgu\l"
# 解析xml ⽂件
xml_tree = ET.parse(xmlPath)
# 获取根节点
root_element = t()
print("根节点名字:", root_element.tag)
# 遍历根节点下的⼦节点限号开车怎么处罚
for sub in root_element:
print("根节点下的字节点:", sub.tag)
for s in sub:
print(s.tag, s.attrib, s.text)
哪里有化妆学校if__name__ == "__main__":
makeXML()
getXML()
结果:
<?xml version='1.0' encoding='utf-8'?>
<Root><sub_elemtnt1><name_0 name="value_0" tag="tag_0">abc0</name_0><name_1 name="value_1" tag="tag_1">abc1</name_1 ><name_2 name="value_2" tag="tag_2">abc2</name_2><name_3 name="value_3" tag="tag_3">abc3</name_3><name_4 name="val ue_4" tag="tag_4">abc4</name_4></sub_elemtnt1><sub_elemtnt2><name_0 name="value_0" tag="tag_0">efg0</name_0><name_1 name="value_1" tag="tag_1">efg1</name_1><name_2 name="value_2" tag="tag_2">efg2</name_2><name_3 name="value_3" tag="t ag_3">efg3</name_3><name_4 name="value_4" tag="tag_4">efg4</name_4></sub_elemtnt2></Root>
<?xml version="1.0" encoding="utf-8"?>
<Root>
<sub_elemtnt1>
<name_0 name="value_0" tag="tag_0">abc0</name_0>
<name_1 name="value_1" tag="tag_1">abc1</name_1>
<name_2 name="value_2" tag="tag_2">abc2</name_2>
<name_3 name="value_3" tag="tag_3">abc3</name_3>
<name_4 name="value_4" tag="tag_4">abc4</name_4>
</sub_elemtnt1>
<sub_elemtnt2>
<name_0 name="value_0" tag="tag_0">efg0</name_0>
<name_1 name="value_1" tag="tag_1">efg1</name_1>
<name_2 name="value_2" tag="tag_2">efg2</name_2>
<name_3 name="value_3" tag="tag_3">efg3</name_3>
<name_4 name="value_4" tag="tag_4">efg4</name_4>
</sub_elemtnt2>
</Root>
根节点名字: Root
根节点下的字节点: sub_elemtnt1
name_0 {'name': 'value_0', 'tag': 'tag_0'} abc0
name_1 {'name': 'value_1', 'tag': 'tag_1'} abc1
name_2 {'name': 'value_2', 'tag': 'tag_2'} abc2
name_3 {'name': 'value_3', 'tag': 'tag_3'} abc3
name_4 {'name': 'value_4', 'tag': 'tag_4'} abc4
根节点下的字节点: sub_elemtnt2
name_0 {'name': 'value_0', 'tag': 'tag_0'} efg0
name_1 {'name': 'value_1', 'tag': 'tag_1'} efg1
暧昧祝福短信name_2 {'name': 'value_2', 'tag': 'tag_2'} efg2
name_3 {'name': 'value_3', 'tag': 'tag_3'} efg3
name_4 {'name': 'value_4', 'tag': 'tag_4'} efg4
>给长辈的生日祝福短信

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。