json 파일을 작성하는 데 어려움이 있습니다. 지금이 코드를파이썬 사전 및 중첩 된 사전/목록에서 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'} ]
}
]
}
,하지만 난 건물입니다 이후 루프는 아직 월, 주, 일처럼 존재하지 않는 값이 발생하는 경우 : 같은 즉 보일 것입니다. 나는 dictionary 값이 존재 하는지를 확인하기 위해 if 문을 사용하려고 노력하고있다. 그렇다면 나무 아래로 계속 진행하고 모든 3에 yes와 yes 및 week가 있는지 확인한 다음 값을 추가한다. 각각에 대한 것이 아니라면 사전/목록에 값을 작성하여 데이터를 채울 수 있도록하십시오.
이제 오류 코드 '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
는 등 친구 같은 다른 변수를 지정하십시오.? – ZdaR
'final_list'는 빈 dict이지만'if final_list [month_bday] :'에 액세스하려고합니까? –
요소가 1 개만있는 정말로 원하는 목록을 만드시겠습니까? – Daniel