我很难建立一个json文件。 那应该是这样的:
{'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'} ]
}
]
}
我现在有这个代码,但是因为我在循环发生时正在构建,所以还有像月,星期和日那样的值。 我正在尝试使用if语句来检查字典值是否存在,如果是,则继续沿着树继续检查是否存在星期,如果是,那么对于所有三个都是,然后附加值。 如果不是每个我都在字典/列表中创建值,以使其可用于填充数据。
现在我收到错误代码' KeyError'。 (字典中不存在值)
这是现在的代码。
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})
编辑:其他变量是: friends_name =' Tony' friends_bday =' 20150516'
感谢Daniel回答我刚刚改变了这个
final_list = defaultdict(lambda: defaultdict(lambda: {}))
到
final_list = defaultdict(lambda: defaultdict(lambda: defaultdict(lambda: {})))
链接到defaultdict函数。 https://docs.python.org/2/library/collections.html#collections.defaultdict
答案 0 :(得分:1)
使用defaultdict
和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
}