Last active
February 21, 2023 22:05
-
-
Save erikvanzijst/cb4b4d929144fb440b391c7894525e63 to your computer and use it in GitHub Desktop.
xmltodict
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
In [1]: import xmltodict | |
In [14]: print(data) | |
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> | |
<server> | |
<response> | |
<status>ok</status> | |
<actions> | |
<action> | |
<subsystem>hourChooser</subsystem> | |
<command>put hours</command> | |
<data> | |
<hours> | |
<custom>0</custom> | |
<hour> | |
<start>09:35</start> | |
<end>08:45</end> | |
<name> </name> | |
</hour> | |
<hour> | |
<start>08:45</start> | |
<end>09:35</end> | |
<name>1e lesuur</name> | |
</hour> | |
<hour> | |
<start>09:35</start> | |
<end>10:25</end> | |
<name>2e lesuur</name> | |
</hour> | |
<hour> | |
<start>10:25</start> | |
<end>11:15</end> | |
<name>3e lesuur</name> | |
</hour> | |
<hour> | |
<start>11:15</start> | |
<end>11:40</end> | |
<name>speeltijd</name> | |
</hour> | |
<hour> | |
<start>11:40</start> | |
<end>12:30</end> | |
<name>4e lesuur</name> | |
</hour> | |
<hour> | |
<start>12:30</start> | |
<end>13:50</end> | |
<name>middagpauze</name> | |
</hour> | |
<hour> | |
<start>13:50</start> | |
<end>14:40</end> | |
<name>5e lesuur</name> | |
</hour> | |
<hour> | |
<start>14:40</start> | |
<end>15:30</end> | |
<name>6e lesuur</name> | |
</hour> | |
</hours> | |
</data> | |
</action> | |
</actions> | |
</response> | |
</server> | |
In [15]: xmltodict.parse(data) | |
Out[15]: | |
{'server': {'response': {'status': 'ok', | |
'actions': {'action': {'subsystem': 'hourChooser', | |
'command': 'put hours', | |
'data': {'hours': {'custom': '0', | |
'hour': [{'start': '09:35', 'end': '08:45', 'name': None}, | |
{'start': '08:45', 'end': '09:35', 'name': '1e lesuur'}, | |
{'start': '09:35', 'end': '10:25', 'name': '2e lesuur'}, | |
{'start': '10:25', 'end': '11:15', 'name': '3e lesuur'}, | |
{'start': '11:15', 'end': '11:40', 'name': 'speeltijd'}, | |
{'start': '11:40', 'end': '12:30', 'name': '4e lesuur'}, | |
{'start': '12:30', 'end': '13:50', 'name': 'middagpauze'}, | |
{'start': '13:50', 'end': '14:40', 'name': '5e lesuur'}, | |
{'start': '14:40', 'end': '15:30', 'name': '6e lesuur'}]}}}}}}} | |
In [17]: print('\n'.join([f"{les['name']}: {les['start']} - {les['end']}" for les in d['server']['response']['actions']['action']['data']['hours']['hour']])) | |
None: 09:35 - 08:45 | |
1e lesuur: 08:45 - 09:35 | |
2e lesuur: 09:35 - 10:25 | |
3e lesuur: 10:25 - 11:15 | |
speeltijd: 11:15 - 11:40 | |
4e lesuur: 11:40 - 12:30 | |
middagpauze: 12:30 - 13:50 | |
5e lesuur: 13:50 - 14:40 | |
6e lesuur: 14:40 - 15:30 | |
In [18]: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
In [23]: request = { | |
...: 'request': { | |
...: 'command': { | |
...: 'subsystem': 'event', | |
...: 'action': 'get hours', | |
...: 'params': { | |
...: 'param': { | |
...: '@name': 'currentdate', | |
...: '#text': '1676212585.984' | |
...: } | |
...: } | |
...: } | |
...: } | |
...: } | |
In [24]: xml = xmltodict.unparse(request, pretty=True) | |
In [25]: print(xml) | |
<?xml version="1.0" encoding="utf-8"?> | |
<request> | |
<command> | |
<subsystem>event</subsystem> | |
<action>get hours</action> | |
<params> | |
<param name="currentdate">1676212585.984</param> | |
</params> | |
</command> | |
</request> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment