So I have an extract from a flat dictionary as shown:
dict= {"active_screen":"artboard_1","artboard_1.content1.grd_x":0,"artboard_2.content2.content3.grd_y":0,"artboard_2.content2.content3.grd_x":0}
I would like to make it hierarchical, like so:
{
'active_screen': 'artboard_1',
'artboard_1': {
'content1': {
'grd_x': 0
}
}
'artboard_2': {
'content2': {
'content3' : {
'grd_y': 0,
'grd_x': 0
}
}
}
}
As you can see, each entry has different structures. I thought that this would be done using recursion, and attempted the following:
import collections
import re
def recursive_dict(collections_default_dict,a,value):
collections_default_dict[a] = value
return collections_default_dict
string = {"active_screen":"artboard_1","artboard_1.content1.grd_x":0,"artboard_2.content2.content3.grd_y":0,"artboard_2.content2.content3.grd_x":0}
e = collections.defaultdict(dict)
for key,value in string.items():
counter=0
matches = re.findall(r"([^.]*)",key)
matches = filter(None, matches)
for a in matches:
recursive_dict(e,a,value)
But this didn't work. Can anyone help me? I need a loop instead of doing it manually since the above is just an extract of a bigger sample.