gpt4 book ai didi

python - 从 python 字典和嵌套字典/列表构建一个 Json 文件

转载 作者:太空宇宙 更新时间:2023-11-04 05:56:20 24 4
gpt4 key购买 nike

我很难构建一个 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

最佳答案

使用defaultdictdatetime.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
}

关于python - 从 python 字典和嵌套字典/列表构建一个 Json 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27581475/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com