딕셔너리에서 데이터 파싱하기
JSON 데이터를 메모리에 로드하면 결과 딕셔너리가 복잡할 수 있어요. 키-값 쌍에 또 다른 딕셔너리가 들어 있는 경우를 중첩 딕셔너리라고 합니다. 이런 중첩 딕셔너리는 API나 기타 JSON 데이터를 다룰 때 자주 마주치게 됩니다. 이 연습 문제에서는 중첩 딕셔너리에서 데이터를 추출하고 누락된 값을 처리하는 방법을 연습해 볼 거예요.
아래 딕셔너리는 school 변수에 저장되어 있어요. 화이팅!
{
"street_address": "111 Columbia Street",
"city": "Manhattan",
"scores": {
"math": 657,
"reading": 601
}
}
이 연습은 강의의 일부입니다
Python으로 ETL과 ELT
연습 안내
school딕셔너리에서"street_address"키에 저장된 값을 파싱하세요.school딕셔너리에서"scores"키에 저장된 값을 파싱하세요.scores딕셔너리에서"math","reading","writing"키의 값을 파싱하고, 기본값은 0으로 설정하세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
# Parse the street_address from the dictionary
street_address = school.____("street_address")
# Parse the scores dictionary
scores = school.____("____")
# Try to parse the math, reading and writing values from scores
math_score = scores.____("math", ____)
reading_score = scores.____
writing_score = ____
print(f"Street Address: {street_address}")
print(f"Math: {math_score}, Reading: {reading_score}, Writing: {writing_score}")