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
}
}
Latihan ini adalah bagian dari kursus
ETL and ELT in Python
Petunjuk latihan
- Parse the value stored at the
"street_address"key from theschooldictionary. - Parse the value stored at the
"scores"key from theschooldictionary. - Parse the values stored at the
"math","reading", and"writing"keys from thescoresdictionary, and set the default value to 0.
Latihan interaktif praktis
Cobalah latihan ini dengan menyelesaikan kode contoh berikut.
# 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}")