python讀寫xml文件


python讀取xml文件

xml文件是具有樹狀結構的,如果想要訪問某個葉子結點,必須逐層獲取其父結點,要讀取某個葉子結點內容用text成員
使用前先加載xml工具包

try: 
  import xml.etree.cElementTree as ET 
except ImportError: 
  import xml.etree.ElementTree as ET 
import sys,os

獲取文件的根結點

tree = ET.parse(filename.xml) 
root = tree.getroot() 

獲取所有為object的元素

root.findall('object')

獲取object中為item的元素

ojbect.find('item')

讀取item元素里面的內容

print item.text


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM