1

I want to make this simpler and be able to search a list of variables: alarm1,alarm2,alarm3... in alarmDict.values()

What I have so far that works:

import xml.etree.ElementTree as ET 
tree = ET.parse (path.filename) #XML file 
root = tree.getroot()


for child in root:
    alarmDict = child.attrib      #This lists the alarm codes as dicts  
    if 'alarm1' in alarmDict.values(): print ('Contains this alarm', 'alarm1')
    if 'alarm2' in alarmDict.values(): print ('Contains this alarm', 'alarm2')
    if 'alarm3' in alarmDict.values(): print ('Contains this alarm', 'alarm3')

What I am going for:

for child in root:
    alarmDict = child.attrib      #This lists the alarm codes as dicts
    alarm_list = [alarm1, alarm2, alarm3]  
    if alarm_list in alarmDict.values(): print ('Contains this alarm', alarm_list[])
braaterAfrikaaner
  • 1,072
  • 10
  • 20
JoshD.py
  • 23
  • 5

1 Answers1

0

You need another loop:

for child in root:
    alarmDict = child.attrib      #This lists the alarm codes as dicts
    alarm_list = [alarm1, alarm2, alarm3]  
    for alarm in alarm_list:
        if alarm in alarmDict.values():
            print('Contains this alarm', alarm)

If you want a really deep dive into the best way for checking list contents against another list, I'd recommend this answer

JacobIRR
  • 8,545
  • 8
  • 39
  • 68