我很难构建一个 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 = '托尼' friends_bday = '20150516'
感谢丹尼尔的回答,我刚刚更改了这个
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
请您参考如下方法:
使用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
}
