温馨提示×

如何使用load函数加载XML格式的数据到Python

小樊
94
2024-08-13 12:33:36
栏目: 编程语言
Python开发者专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

在Python中,可以使用xml.etree.ElementTree模块中的ElementTree类来加载XML格式的数据。以下是一个简单的示例:

import xml.etree.ElementTree as ET

# 加载XML文件
tree = ET.parse('data.xml')
root = tree.getroot()

# 遍历XML数据
for child in root:
    print(child.tag, child.attrib)
    for subchild in child:
        print(subchild.tag, subchild.text)

在上面的示例中,首先使用ET.parse函数加载XML文件,然后可以通过getroot方法获取XML树的根节点。接下来可以使用循环遍历XML数据,并访问子节点和属性。

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读:如何使用load函数加载JSON格式的数据到Python

0