Dictionaries से डेटा पार्स करना
जब JSON डेटा मेमोरी में लोड होता है, तो बनने वाली dictionary जटिल हो सकती है। Key-value पेयर्स के भीतर एक और dictionary हो सकती है — इन्हें nested dictionaries कहा जाता है। APIs या अन्य JSON डेटा के साथ काम करते समय ये nested dictionaries अक्सर मिलती हैं। इस अभ्यास में, आप nested dictionaries से डेटा निकालने और missing values को संभालने का अभ्यास करेंगे.
नीचे दी गई dictionary school वैरिएबल में स्टोर है। शुभकामनाएँ!
{
"street_address": "111 Columbia Street",
"city": "Manhattan",
"scores": {
"math": 657,
"reading": 601
}
}
यह अभ्यास पाठ्यक्रम का हिस्सा है
Python में ETL और ELT
अभ्यास निर्देश
schooldictionary से"street_address"key पर स्टोर मान को पार्स करें.schooldictionary से"scores"key पर स्टोर मान को पार्स करें.scoresdictionary से"math","reading", और"writing"keys पर स्टोर मानों को पार्स करें, और डिफ़ॉल्ट मान 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}")