-
Notifications
You must be signed in to change notification settings - Fork 2
/
convertdataformat.py
70 lines (57 loc) · 1.54 KB
/
convertdataformat.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import json
import xmltodict
from pprint import pprint
import yaml
import collections
def deep_convert_dict(layer):
to_ret = layer
if isinstance(layer, collections.OrderedDict):
to_ret = dict(layer)
try:
for key, value in to_ret.items():
if isinstance(value,list):
newvalue= []
for x in value:
newvalue.append(deep_convert_dict(x))
value= newvalue
to_ret[key] = deep_convert_dict(value)
except AttributeError:
pass
return to_ret
xmldoc='''
<breakfast_menu>
<food>
<name>Belgian Waffles</name>
<price>$5.95</price>
<description>
Two of our famous Belgian Waffles with plenty of real maple syrup
</description>
<calories>650</calories>
</food>
<food>
<name>French Toast</name>
<price>$4.50</price>
<description>
Thick slices made from our homemade sourdough bread
</description>
<calories>600</calories>
</food>
<food>
<name>Homestyle Breakfast</name>
<price>$6.95</price>
<description>
Two eggs, bacon or sausage, toast, and our ever-popular hash browns
</description>
<calories>950</calories>
</food>
</breakfast_menu>
'''
pythonstruct = dict(xmltodict.parse(xmldoc))
# remove ordered dict
jsonstruct= json.dumps(pythonstruct, sort_keys=True, indent=4, separators=(',', ': '))
yamlstruct= yaml.safe_dump(deep_convert_dict(pythonstruct), default_flow_style=False)
struct= [pythonstruct,jsonstruct,yamlstruct]
for i in struct:
print(i)
print('###')
import ipdb; ipdb.set_trace()