การวนลูปผ่านดิกชันนารี
เมื่อโหลดข้อมูล JSON เข้าสู่ดิกชันนารีแล้ว คุณสามารถใช้เครื่องมือในตัวของ Python เพื่อวนลูปผ่านคีย์และค่าต่าง ๆ ของดิกชันนารีนั้นได้
ไฟล์ "nested_school_scores.json" ถูกอ่านเข้าสู่ดิกชันนารีที่เก็บไว้ในตัวแปร raw_testing_scores ซึ่งมีรูปแบบดังนี้:
{
"01M539": {
"street_address": "111 Columbia Street",
"city": "Manhattan",
"scores": {
"math": 657,
"reading": 601,
"writing": 601
}
}, ...
}
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
ETL and ELT ใน Python
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
raw_testing_scores_keys = []
# Iterate through the keys of the raw_testing_scores dictionary
for school_id in raw_testing_scores.____():
# Append each key to the raw_testing_scores_keys list
raw_testing_scores_keys.append(____)
print(raw_testing_scores_keys[0:3])