日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

python 遍历xml所有节点

發布時間:2025/4/16 48 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python 遍历xml所有节点 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.xml文件

2.代碼

#coding:utf-8 import xml import xml.etree.ElementTree as ET""" 實現從xml文件中讀取數據 """ #全局唯一標識 unique_id = 1#遍歷所有的節點 def walkData(root_node, level, result_list): global unique_id temp_list =[unique_id, level, root_node.tag, root_node.attrib] result_list.append(temp_list) unique_id += 1#遍歷每個子節點 children_node = root_node.getchildren() if len(children_node) == 0: returnfor child in children_node: walkData(child, level + 1, result_list) returndef getXmlData(file_name): level = 1 #節點的深度從1開始 result_list = [] root = ET.parse(file_name).getroot() walkData(root, level, result_list) return result_list if __name__ == '__main__':#'d:\\fenlei2.xml'file_name = 'd:\\222.xml'R = getXmlData(file_name) for x in R: print(x)pass

?3.執行結果

?

轉載于:https://www.cnblogs.com/lisa2016/p/10419551.html

《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀

總結

以上是生活随笔為你收集整理的python 遍历xml所有节点的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。