Skip to content

Instantly share code, notes, and snippets.

@Roger8
Created July 10, 2018 01:46
Show Gist options
  • Save Roger8/2de82dd561abce8464bd77d56e66c2cb to your computer and use it in GitHub Desktop.
Save Roger8/2de82dd561abce8464bd77d56e66c2cb to your computer and use it in GitHub Desktop.
python xml parsing
import xml.etree.ElementTree as ET
tree = ET.parse('train_11pts.xml')
rt = tree.getroot()
# 根据路径解析 ./images/image
im = rt.findall('./images/image')
b = imgs[0].find('./box')
print len(b)
# 列出b的子元素
# list(b) , b.items()
print b[0].attrib
# {'name': '00', 'x': '66', 'y': '182'}
# 获取某个属性
print b[0].attrib['name']
<dataset>
<name>iBUG face point dataset - training images</name>
<comment>This folder contains data downloaded from:
http://ibug.doc.ic.ac.uk/resources/facial-point-annotations/.</comment>
<images>
  <image file="lfpw/trainset/image_0457.png">
    <box height="140" left="74" top="78" width="138">
      <part name="00" x="66" y="182" />
      <part name="01" x="186" y="152" />
      <part name="02" x="163" y="168" />
      <part name="03" x="120" y="129" />
      <part name="04" x="121" y="135" />
      <part name="05" x="169" y="122" />
      <part name="06" x="170" y="129" />
      <part name="07" x="139" y="194" />
      <part name="08" x="163" y="182" />
      <part name="09" x="176" y="189" />
      <part name="10" x="165" y="199" />
      </box>
  </image>
 <image file="helen/trainset/2659264056_1.jpg">
    <box height="447" left="31" top="130" width="447">
      <part name="00" x="97" y="314" />
      <part name="01" x="526" y="431" />
      <part name="02" x="241" y="427" />
      <part name="03" x="195" y="224" />
      <part name="04" x="185" y="252" />
      <part name="05" x="399" y="276" />
      <part name="06" x="394" y="304" />
      <part name="07" x="168" y="468" />
      <part name="08" x="233" y="471" />
      <part name="09" x="314" y="511" />
      <part name="10" x="225" y="515" />
      </box>
  </image>
</images>
</dataset>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment