close

假設有一個test.xml,

內容大致如下

接著我們要如何讀取這份檔案內容呢?

這邊使用到xml的模組,

假設我們只想要拿到object的相關參數,

寫法如下:

import xml.etree.ElementTree as ET

xml = ET.parse('test.xml')

root = xml.getroot()

obj = root.findall('object')

name = obj.find('name').text

bndbox = obj.find('bndbox')

xmin = int(bndbox[0].text)

ymin = int(bndbox[1].text)

xmax = int(bndbox[2].text)

ymax = int(bndbox[3].text)

 

讀取xml方式猶如剝洋蔥一樣,

一層層剝開即可

 

arrow
arrow
    文章標籤
    python xml
    全站熱搜

    楓綺 發表在 痞客邦 留言(0) 人氣()