Python Forum

Full Version: ElementTree get attribute value part of string
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi all,

I have this :

>>> import xml.etree.ElementTree as ET
>>> tree = ET.parse('disks.xml')
>>> root = tree.getroot()
>>> root.tag
'cli-output'
>>> root.attrib
{}
>>> for context in root.iter('context'):
...     print(context.attrib)
...
{'cell': 'celadm04'}
But I want to get only this value "celadm04" and add to a variable.

How can I do that using ElementTree? I tried find and findall but did not work.
context.attrib is a dictionary. "cell" is the key, and "celadm04" the value. context.attrib["cell"] would return "celadm04".

This talks about Python dictionaries

https://docs.python.org/3/tutorial/datas...ctionaries
https://python-reference.readthedocs.io/...docs/dict/