Posts: 28
Threads: 16
Joined: Mar 2022
Hi all,
I'm new to Python. I have this :
>>> import xml.etree.ElementTree as ET
>>> xml_to_parse1 = ET.parse('/tmp/xmlfile.xml')
>>> get_xml = xml_to_parse1.getroot()
>>> for x in get_xml.findall('physicaldisk'):
... diskType =x.find('diskType').text
... print(diskType)
...
HardDisk
HardDisk
HardDisk
HardDisk
HardDisk
HardDisk
HardDisk
HardDisk
HardDisk
HardDisk
HardDisk
HardDisk
FlashDisk
FlashDisk
FlashDisk
FlashDisk
FlashDisk
FlashDisk
FlashDisk
FlashDisk
M2Disk
M2Disk
PMEM
PMEM
PMEM
PMEM
PMEM
PMEM
PMEM
PMEM
PMEM
PMEM
PMEM
PMEM
>>> I want add all these values (remove duplicate) to a LIST. How to do that in python?
Posts: 1,566
Threads: 3
Joined: Mar 2020
Mar-09-2022, 01:25 AM
(This post was last modified: Mar-09-2022, 01:26 AM by bowlofred.)
If you want to get rid of duplicates, think "set". Just add() things to it and it will only store the unique elements.
>>> diskTypes = set()
>>> diskTypes.add("HardDisk")
>>> diskTypes.add("HardDisk")
>>> diskTypes.add("HardDisk")
>>> diskTypes.add("FlashDisk")
>>> diskTypes.add("FlashDisk")
>>> diskTypes.add("HardDisk")
>>> diskTypes
{'FlashDisk', 'HardDisk'}
Posts: 6,221
Threads: 16
Joined: Feb 2020
Mar-09-2022, 04:04 AM
(This post was last modified: Mar-09-2022, 04:05 AM by deanhystad.)
Or a Counter dictionary. That gets you all the types and a count of how many of each type.
Something like this:
Output: >>> from collections import Counter
>>> import xml.etree.ElementTree as ET
>>> xml_to_parse1 = ET.parse('/tmp/xmlfile.xml')
>>> get_xml = xml_to_parse1.getroot()
>>> disktypes = Counter([x.find('diskType') for x in get_xml.findall('physiccaldisk')])
>>> print(disktypes)
Posts: 28
Threads: 16
Joined: Mar 2022
(Mar-09-2022, 04:04 AM)deanhystad Wrote: Or a Counter dictionary. That gets you all the types and a count of how many of each type.
Something like this:
Output: >>> from collections import Counter
>>> import xml.etree.ElementTree as ET
>>> xml_to_parse1 = ET.parse('/tmp/xmlfile.xml')
>>> get_xml = xml_to_parse1.getroot()
>>> disktypes = Counter([x.find('diskType') for x in get_xml.findall('physiccaldisk')])
>>> print(disktypes)
When I run this I got many "Elements" words as below:
Quote:>>> from collections import Counter
>>> import xml.etree.ElementTree as ET
>>> xml_to_parse1 = ET.parse('/tmp/xmlfile.xml')
>>> get_xml = xml_to_parse1.getroot()
>>> disktypes = Counter([x.find('diskType') for x in get_xml.findall('physiccaldisk')])
>>> disktypes = Counter([x.find('diskType') for x in get_xml.findall('physicaldisk')])
>>> print(disktypes)
Counter({<Element 'diskType' at 0x7f62fdfd6ed0>: 1, <Element 'diskType' at 0x7f62fdfd60d0>: 1, <Element 'diskType' at 0x7f62fdfc3ed0>: 1, <Element 'diskType' at 0x7f62fe00c110>: 1, <Element 'diskType' at 0x7f62fe00c590>: 1, <Element 'diskType' at 0x7f62fdfc3190>: 1, <Element 'diskType' at 0x7f62fe0389d0>: 1, <Element 'diskType' at 0x7f62fdfd6450>: 1, <Element 'diskType' at 0x7f62fe00ca10>: 1, <Element 'diskType' at 0x7f62fe038250>: 1, <Element 'diskType' at 0x7f62fe02da90>: 1, <Element 'diskType' at 0x7f62fe02d310>: 1, <Element 'diskType' at 0x7f62fe018350>: 1, <Element 'diskType' at 0x7f62fe038d90>: 1, <Element 'diskType' at 0x7f62fdfc3b50>: 1, <Element 'diskType' at 0x7f62fe07fc10>: 1, <Element 'diskType' at 0x7f62fe018c50>: 1, <Element 'diskType' at 0x7f62fe023a10>: 1, <Element 'diskType' at 0x7f62fdfc34d0>: 1, <Element 'diskType' at 0x7f62fdfccd10>: 1, <Element 'diskType' at 0x7f62fdfcc610>: 1, <Element 'diskType' at 0x7f62fdfcc990>: 1, <Element 'diskType' at 0x7f62fe023590>: 1, <Element 'diskType' at 0x7f62fdfd67d0>: 1, <Element 'diskType' at 0x7f62fe038610>: 1, <Element 'diskType' at 0x7f62fe02de50>: 1, <Element 'diskType' at 0x7f62fe023110>: 1, <Element 'diskType' at 0x7f62fe00ce90>: 1, <Element 'diskType' at 0x7f62fe02d6d0>: 1, <Element 'diskType' at 0x7f62fdfc37d0>: 1, <Element 'diskType' at 0x7f62fdfd6b50>: 1, <Element 'diskType' at 0x7f62fdfcc290>: 1, <Element 'diskType' at 0x7f62fe023e90>: 1, <Element 'diskType' at 0x7f62fe0187d0>: 1})
Posts: 6,221
Threads: 16
Joined: Feb 2020
x.find('diskType').text # Missed the .text I did say "Something like this"
|