Newbie XML problem

Miki Tebeka miki.tebeka at gmail.com
Tue Dec 22 00:49:43 EST 2015


Hi,

> config = {id: 1, canvas: (3840, 1024), comment: "a comment",
>              {id: 4, gate: 3, (0,    0, 1280, 1024)},   
>              {id: 5, gate: 2, (1280, 0, 2560, 1024)},
>              {id: 6, gate: 1, (2560, 0, 3840, 1024)}}
This is not valid Python. Are you trying to have a list of dicts?

> I have started to use this code - but this is beginning to feel very non-elegant; not the cool Python code I usually see...
ElementTree supports XPATH, using this and some list comprehension you do things a little more easily. For example:

    cfg = root.find('config[@id="B"]')
    for panel in cfg.findall('panel'):
        panels.append([{elem.tag: elem.text} for elem in panel])

You'll need to do some parsing for the coordinates and handle canvas and comments separately.
      
HTH,
Miki



More information about the Python-list mailing list