0

I am trying to read the filename of an image from the XML file I run the following code:

import xml.etree.ElementTree as ET
tree = ET.parse(r'...\57128.xml')
root = tree.getroot()
a = root.find('.//filename')
print(a)

But instead of 57128.png I get this output: <Element 'filename' at 0x000001ED3297C4A8> This is what the xml file contains:

<annotation>
  <folder>save</folder>
  <filename>57128.png</filename>
  <size>
    <width>1280</width>
    <height>720</height>
    <depth>3</depth>
  </size>

What else can I do to get the filename as it is written in the XML file?

desertnaut
  • 57,590
  • 26
  • 140
  • 166

2 Answers2

0

Problem solved I used

a = root.find('.//filename').text

and it worked

0

Use findtext:

filename = root.findtext('filename')
Daniel
  • 42,087
  • 4
  • 55
  • 81