python解析xml檔案——通過etree來解析xml檔案
利用from lxml import etree來解析
---------------------------這個是有揹包的情況下---------------------------------
<?xmlversion="1.0" ?>
<annotation>
<size>
<width>75</width>
<height>177</height>
<depth>3</depth>
</size>
<gender>1</gender>
<hairlength>0</hairlength>
<object>
<name>head</name>
<bndbox>
<xmin>38</xmin>
<ymin>1</ymin>
<xmax>58</xmax>
<ymax>26</ymax>
</bndbox>
</object>
<object>
<name>top</name>
<category>3</category>
<color>8</color>
<bndbox>
<xmin>17</xmin>
<ymin>23</ymin>
<xmax>71</xmax>
<ymax>106</ymax>
</bndbox>
</object>
<object>
<name>down</name>
<category>0</category>
<color>0</color>
<bndbox>
<xmin>30</xmin>
<ymin>105</ymin>
<xmax>57</xmax>
<ymax>121</ymax>
</bndbox>
</object>
<object>
<name>shoes</name>
<category>3</category>
<color>7</color>
<bndbox>
<xmin>29</xmin>
<ymin>122</ymin>
<xmax>42</xmax>
<ymax>168</ymax>
</bndbox>
</object>
<object>
<name>shoes</name>
<category>3</category>
<color>7</color>
<bndbox>
<xmin>43</xmin>
<ymin>122</ymin>
<xmax>57</xmax>
<ymax>168</ymax>
</bndbox>
</object>
<object>
<name>bag</name>
<category>0</category>
<color>7</color>
<bndbox>
<xmin>9</xmin>
<ymin>55</ymin>
<xmax>27</xmax>
<ymax>84</ymax>
</bndbox>
</object>
</annotation>
-------------------------------------------這個是沒有揹包的情況下:此時在object標籤下面沒有bag這個label----------------------
<?xmlversion="1.0" ?>
<annotation>
<size>
<width>66</width>
<height>199</height>
<depth>3</depth>
</size>
<gender>1</gender>
<hairlength>0</hairlength>
<object>
<name>head</name>
<bndbox>
<xmin>23</xmin>
<ymin>2</ymin>
<xmax>50</xmax>
<ymax>34</ymax>
</bndbox>
</object>
<object>
<name>top</name>
<category>2</category>
<color>2</color>
<bndbox>
<xmin>1</xmin>
<ymin>35</ymin>
<xmax>63</xmax>
<ymax>132</ymax>
</bndbox>
</object>
<object>
<name>down</name>
<category>0</category>
<color>0</color>
<bndbox>
<xmin>9</xmin>
<ymin>131</ymin>
<xmax>39</xmax>
<ymax>167</ymax>
</bndbox>
</object>
<object>
<name>shoes</name>
<category>3</category>
<color>0</color>
<bndbox>
<xmin>26</xmin>
<ymin>164</ymin>
<xmax>46</xmax>
<ymax>195</ymax>
</bndbox>
</object>
<object>
<name>shoes</name>
<category>3</category>
<color>0</color>
<bndbox>
<xmin>11</xmin>
<ymin>174</ymin>
<xmax>28</xmax>
<ymax>193</ymax>
</bndbox>
</object>
</annotation>
Q1:
遇到的第一個難題就是:如何解析<gender>以及<hairlength>,
多虧了https://zhidao.baidu.com/question/523477874624497285.html
利用findall方法,將gender以及hairlength屬性名來進行解析,pass
Q2:如何解析根節點下面的子節點的內容:
http://blog.csdn.net/oxiangduinishuo1/article/details/51864503這個主要是教理論的
http://www.cnblogs.com/hongten/p/hongten_python_xml_etree_elementtree.html,參考這個教程,寫出了圖片的size,但是有個問題:多個object標籤怎麼處理
http://techpool.iteye.com/blog/643667,可以通過if判斷標籤名字來解決,這個連結給我了靈感
Q3:卡了半天,因為不同的檔案,是否揹包的屬性不同,有的檔案沒有書包這個屬性,因此我直接做了一個obj_name_list,將所有的屬性都放到這個list當中了,然後進行判斷是否bag這個屬性在list當中,如果不在的話,那就像讀取第二個xml檔案那樣了
-----------------------------最後貼一下程式碼-----------------------------------------
# -*- coding: utf-8 -*-
__author__ = 'xuy'
from lxml import etree
gender_list=['male','female']
hair_list=['long','short','other']
top_list=['T-shirt','skirt','waitao','rurongfu','xifu','other']#上衣
down_list=['changku','duanku','changqun','duanqun','other']
shoes_list=['pixie','yundongxie','liangxie','xuezi','other']
bag_list=['danjianbao','shuangjianbao','shoulaxiang','qianbao','other']
color_list=['black','white','red','yellow','blue','green','purpose','brown','gray','orange','multi_color','other']
xml_file=etree.parse('IMG_000009.xml')
root_node=xml_file.getroot()
def gender_hair_node(type):
for atype in root_node.findall(type):
attr=int(atype.text)
return attr
"""
輸出檔案的大小
"""
for size_node in root_node.findall('size'):
pic_width=int(size_node.find('width').text)
pic_height=int(size_node.find('height').text)
pic_depth=int(size_node.find('depth').text)
print "圖片大小:%d",pic_width
print "圖片大小:%d",pic_height
print "圖片大小:%d",pic_depth
"""
輸出性別以及頭髮的長短
"""
gender_arr=gender_list[gender_hair_node('gender')]
hair_arr=hair_list[gender_hair_node('hairlength')]
print gender_arr
print hair_arr
#未對與函式進行封裝的時候
# for atype_gender in root_node.findall('gender'):
# gender_attr=gender_list[int(atype_gender.text)]
# print gender_attr
#
# for atype_hair in root_node.findall('hairlength'):
# hair_attr=hair_list[int(atype_hair.text)]
# print hair_attr
"""
獲取head的相關資訊,head雖然在object裡面,但是沒有category以及color的屬性,因此單獨拿出來
"""
all_object_node=root_node.findall('object')
for obj_node in all_object_node:
obj_name=obj_node.find('name').text
if obj_name=='head':
head_obj_name=obj_name+'_head'
for bndbox_node in obj_node.findall('bndbox'):
head_xmin=int(bndbox_node.find('xmin').text)
head_ymin=int(bndbox_node.find('ymin').text)
head_xmax=int(bndbox_node.find('xmax').text)
head_ymax=int(bndbox_node.find('ymax').text)
print head_xmin
print head_ymin
print head_xmax
print head_ymax
print "-----------------------------------------------"
all_object_node=root_node.findall('object')
def object_node(node_name):
all_object_node=root_node.findall('object')
for obj_node in all_object_node:
obj_name=obj_node.find('name').text#找到了obj_name,根據obj_name來區分屬性
if obj_name==node_name:#如果輸入的引數符合obj_name的話,那就直接進入該屬性label裡面
#---------------------------------------------------------
category=int(obj_node.find('category').text)
#---------------------------------------------------------
color=int(obj_node.find('color').text)
#---------------------------------------------------------
for bndbox_node in obj_node.findall('bndbox'):
xmin=int(bndbox_node.find('xmin').text)
ymin=int(bndbox_node.find('ymin').text)
xmax=int(bndbox_node.find('xmax').text)
ymax=int(bndbox_node.find('ymax').text)
print node_name#更新它的obj的名字
print category
print color
print xmin
print ymin
print xmax
print ymax
return node_name,category,color,xmin,ymin,xmax,ymax
#---------------------------------------------------------
print "-----------------------------------------------"
top_node_name,top_category,top_color,top_xmin,top_ymin,top_xmax,top_ymax=object_node('top')
print "-----------------------------------------------"
down_node_name,down_category,down_color,down_xmin,down_ymin,down_xmax,down_ymax=object_node('down')
print "-----------------------------------------------"
shoes_node_name,shoes_category,shoes_color,shoes_xmin,shoes_ymin,shoes__xmax,shoes_ymax=object_node('shoes')
print "-----------------------------------------------"
"""
因為涉及到是否揹包,如果不揹包的話,xml檔案裡面沒有資料集,因此單獨處理
"""
all_object_node=root_node.findall('object')
has_bag=False
for obj_node in all_object_node:
obj_name=obj_node.find('name').text
obj_name_list=[]
obj_name_list.append(obj_name)
if obj_name=='bag':#資料集當中有bag,那麼就讀取bag當中的畫素區域
has_bag=True
for bndbox_node in obj_node.findall('bndbox'):
bag_xmin=int(bndbox_node.find('xmin').text)
bag_ymin=int(bndbox_node.find('ymin').text)
bag_xmax=int(bndbox_node.find('xmax').text)
bag_ymax=int(bndbox_node.find('ymax').text)
if 'bag' not in obj_name_list:
bag_xmin=None
bag_ymin=None
bag_xmax=None
bag_ymax=None
print '是否揹包:',has_bag
print bag_xmin
print bag_ymin
print bag_xmax
print bag_ymax
總結:應該再看一下etree的官方文件,基本上是第二次寫xml解析了,還不是特別熟練,以後應該多加練習