2

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

7
  • Please specify the other variables like friends,etc.? Commented Dec 20, 2014 at 15:10
  • final_list is an empty dict, yet you're trying to access if final_list[month_bday]:? Commented Dec 20, 2014 at 15:11
  • Do your really want lists with only 1 element? Commented Dec 20, 2014 at 15:17
  • why not check for the key before you try to access it? Commented Dec 20, 2014 at 15:21
  • @Anmol_uppal, I updated the other variables. Rawing I was trying to check if the value in the dict exist if not then create it. Commented Dec 20, 2014 at 15:49

1 Answer 1

1

Use defaultdicts and datetime.strptime:

from collections import defaultdict
final_list = defaultdict(lambda: defaultdict(lambda: {}))

for friend in friends:
    friends_name = friend['SUMMARY'][16:]
    friends_bdate = datetime.datetime.strptime(friend['DTSTART'], '%Y%m%d')
    week_number = friends_bdate.replace(year=2015).isocalendar()[1]
    final_list[friends_bdate.month][week_number][friends_bdate.day] = {
        'name': friends_name, 'bdate': friend['DTSTART'], 'pic' : friends_picture
    }
Sign up to request clarification or add additional context in comments.

1 Comment

thank you ! I only an additional lambda (for day value), final_list = defaultdict(lambda: defaultdict(lambda: defaultdict(lambda: {})))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.