시작하기무료로 시작하기

딕셔너리에서 데이터 파싱하기

JSON 데이터를 메모리에 불러오면 그 결과로 만들어지는 딕셔너리가 복잡한 구조를 가질 수 있습니다. 키-값 쌍 안에 또 다른 딕셔너리가 포함될 수 있는데, 이를 중첩 딕셔너리(Nested Dictionary)라고 부릅니다. 이러한 중첩 딕셔너리는 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}")
코드 편집 및 실행