Aan de slagGa gratis aan de slag

Parsing data from dictionaries

When JSON data is loaded into memory, the resulting dictionary can be complicated. Key-value pairs may contain another dictionary, such are called nested dictionaries. These nested dictionaries are frequently encountered when dealing with APIs or other JSON data. In this exercise, you will practice extracting data from nested dictionaries and handling missing values.

The dictionary below is stored in the school variable. Good luck!

{
    "street_address": "111 Columbia Street",
    "city": "Manhattan",
    "scores": {
        "math": 657,
        "reading": 601
    }
}

Deze oefening maakt deel uit van de cursus

ETL and ELT in Python

Cursus bekijken

Oefeninstructies

  • Parse the value stored at the "street_address" key from the school dictionary.
  • Parse the value stored at the "scores" key from the school dictionary.
  • Parse the values stored at the "math", "reading", and "writing" keys from the scores dictionary, and set the default value to 0.

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

# 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}")
Code bewerken en uitvoeren