Python Forum
ElementTree get attribute value part of string
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
ElementTree get attribute value part of string
#1
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.
Reply
#2
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/
paulo79 likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [SOLVED] [ElementTree] Grab text in attributes? Winfried 3 3,211 May-27-2022, 04:59 PM
Last Post: Winfried
  [ElementTree] Insert big block of HTML? Winfried 0 2,038 May-12-2022, 07:08 AM
Last Post: Winfried
  AttributeError: module 'string' has no attribute 'uppercase' Anldra12 10 15,872 Apr-23-2021, 05:30 PM
Last Post: ibreeden
  xml.etree.ElementTree question. water 0 4,710 Oct-09-2020, 06:47 PM
Last Post: water
  xml.etree.ElementTree extract string values matthias100 2 9,659 Jul-12-2020, 06:02 PM
Last Post: snippsat
  Python 3 Elementtree and Comment First gw1500se 3 6,331 May-25-2020, 09:02 PM
Last Post: gw1500se
  Write the XML file from elementtree with hexa decimal encoding Dillibabu 4 5,450 Dec-24-2019, 10:10 AM
Last Post: Dillibabu
  rjust part of a string pythonprogrammer 2 3,356 Sep-17-2019, 12:44 AM
Last Post: snippsat
  How to get values from part of a string? mahi926 1 2,682 May-16-2019, 07:27 AM
Last Post: buran
  Please help me error "AttributeError: 'module' object has no attribute 'ElementTree'" mattroi261192 4 9,606 Nov-11-2018, 05:01 AM
Last Post: mattroi261192

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.