XML : My Complex Dictionary is Jumbling up

Question: I am trying to make the wanted result from the provided XML. In the attempted code all the dictionaries jumble up and in the bar key I get ipsum values.

Wanted Result:

  {'bar': [{'a': 'foo', 'b': 'bar', 'c': 'bazz', 'd': 'boo', 'e': 'bing'}, {'a': 'foo', 'b': 'bar', 'c': 'bazz', 'd': 'boo'}], 'ipsum': {'a': 'lorem', 'b': 'ipsum', 'c': 'dolar', 'd': 'sit', 'e': 'amet'}, {'a': 'lorem', 'b': 'ipsum', 'c': 'dolor', 'd': 'sit'}    

Extra details: Yes i want to remove the None value of foobar for the first part of the list in the dictionaries value. Yes the second part of of the dictionaries is foobar

Attempted Code:

  data = {}  toplevel = {}  secondlevel = {}  details = []    for alphabet in root.findall("*[@type='3']"):      toplevel.clear()      secondevel.clear()        for i in alphabet:          toplevel.update({i.tag: i.text})          toplevel = {k: v for k, v in toplevel.items() if v} #Remove Keys if the value equals None or "          /n" (Doesn't work)        for foobar in toplevel.find("foobar"):          secondlevel.update({foobar.tag: foobar.text})          continue          details.append(toplevel, secondlevel])        data.update({alphabet.find("b").text: details})    

XML:

  <lorem>      <foo type="3">          <a>foo</a>          <b>bar</b>          <c>bazz</c>          <d>boo</d>          <e>bing</e>            <foobar>              <a>foo</a>              <b>bar</b>              <c>bazz</c>              <d>boo</d>          <foobar>      </foo>        <foo type="3">          <a>lorem</a>          <b>ipsum</b>          <c>dolor</c>          <d>sit/d>          <e>amet</e>            <foobar>              <a>lorem</a>              <b>ipsum</b>              <c>dolor</c>              <d>sit</d>          <foobar>      </foo>  </lorem>    

No comments:

Post a Comment