I am having hard time to build a json file. That should look like:
{'January':
[ {'week 1' :
[ {'day 1': [{'birthday_name': 'Stack', 'lastname': 'hector'},{'birthday_name': 'Stack', 'lastname': 'hector'},
{'day 2': [{'birthday_name': 'Vlad', 'lastname': 'Overflow'},{'birthday_name': 'Exchange', 'lastname': 'Other'} ]
}
],
[ {'week 2' :
[ {'day 1': [{'birthday_name': 'Stack', 'lastname': 'hector'},{'birthday_name': 'Stack', 'lastname': 'hector'},
{'day 2': [{'birthday_name': 'Vlad', 'lastname': 'Overflow'},{'birthday_name': 'Exchange', 'lastname': 'Other'} ]
}
]
}
I have this code for now, but since I am building when the loop occurs there is value that do not exist yet like month, week and day. I am trying with if statement to check if the dictionnary value exist, if yes continue down the tree and check if there is the week and the day if yes to all three then append the value. If not for each of them I create the value in the dictionary/list to make it available to fill then the data.
And now I get error code 'KeyError'. (value does not exist in dictionary)
here is the code for now.
final_list = {}
for i in friends:
friends_name = i['SUMMARY'][16:]
friends_bdate = i['DTSTART']
month_bday = int(i['DTSTART'][4:6])
day_bday = int(i['DTSTART'][6:8])
week_number = datetime.date(2015, month_bday, day_bday).isocalendar()[1]
if final_list[month_bday]:
if final_list[month_bday][week_number]:
if final_list[month_bday][week_number][day_bday]:
final_list[month_bday][week_number][day_bday].append({'name': friends_name, 'bdate': friends_bdate, 'pic' : friends_picture})
else:
final_list[month_bday] = [{week_number: [{day_bday: []}]}]
final_list[month_bday][week_number][day_bday].append({'name': friends_name, 'bdate': friends_bdate, 'pic' : friends_picture})
else:
final_list[month_bday] = [{week_number: []}]
if final_list[month_bday][week_number][day_bday] :
final_list[month_bday][week_number][day_bday].append({'name': friends_name, 'bdate': friends_bdate, 'pic' : friends_picture})
else:
final_list[month_bday] = [{week_number: [{day_bday: []}]}]
final_list[month_bday][week_number][day_bday].append({'name': friends_name, 'bdate': friends_bdate, 'pic' : friends_picture})
else:
final_list[month_bday] = {month_bday : []}
if final_list[month_bday][week_number]:
if final_list[month_bday][week_number][day_bday]:
final_list[month_bday][week_number][day_bday].append({'name': friends_name, 'bdate': friends_bdate, 'pic' : friends_picture})
else:
final_list[month_bday] = [{week_number: [{day_bday: []}]}]
final_list[month_bday][week_number][day_bday].append({'name': friends_name, 'bdate': friends_bdate, 'pic' : friends_picture})
else:
final_list[month_bday] = [{week_number: []}]
if final_list[month_bday][week_number][day_bday] :
final_list[month_bday][week_number][day_bday].append({'name': friends_name, 'bdate': friends_bdate, 'pic' : friends_picture})
else:
final_list[month_bday] = [{week_number: [{day_bday: []}]}]
final_list[month_bday][week_number][day_bday].append({'name': friends_name, 'bdate': friends_bdate, 'pic' : friends_picture})
edit: the others variable are: friends_name = 'Tony' friends_bday = '20150516'
Thank for Daniel answer I just changed this
final_list = defaultdict(lambda: defaultdict(lambda: {}))
to
final_list = defaultdict(lambda: defaultdict(lambda: defaultdict(lambda: {})))
link to defaultdict function. https://docs.python.org/2/library/collections.html#collections.defaultdict
final_listis an empty dict, yet you're trying to accessif final_list[month_bday]:?