Context:
I want to obtain information from an xml file. when doing print
"<Element 'vocation' at 0x000001C9C7849040>" I do not want that message to appear but the value
Original file XML:
<?xml version="1.0"?>
<spells>
<instant name="spell 1">
<vocation name="men 50"/>
<vocation name="men 100"/>
</instant>
<instant name="spells 2">
<vocation name="woman 50"/>
<vocation name="woman 100"/>
</instant>
</spells>
python code proposition
import xml.etree.ElementTree as ET
tree = ET.parse('D:\\desktop\\Archivos\\compilaciones\\1.5\\800\\data\\spells\\spells.xml')
root = tree.getroot()
for i in root.findall('instant'):
name_spells = i.get('name')
value_name_vocation = i.find('.//vocation[last()]')
print(name_spells, value_name_vocation)
Output Problem(<Element 'vocation' at 0x000001C9C7849040>)
spells1 <Element 'vocation' at 0x000001C9C7AB9810>
spells2 <Element 'vocation' at 0x000001C9C7849040开发者_StackOverflow社区>
desirable output
spells1 men 100
spells2 woman 100
I was searching and some functions don't work.
.text -> AttributeError: 'NoneType' object has no attribute 'text'
->'NoneType' object has no attribute 'iter'
I am open to using other libraries
Since you have the element object already you just need to get its name from within it. Like you already do with your counter variable i
.
import xml.etree.ElementTree as ET
tree = ET.parse('D:\\desktop\\Archivos\\compilaciones\\1.5\\800\\data\\spells\\spells.xml')
root = tree.getroot()
for i in root.findall('instant'):
name_spells = i.get('name')
value_name_vocation = i.find('.//vocation[last()]')
print(name_spells, value_name_vocation.get('name'))
精彩评论