我使用此函數(shù)讀取 Instagram JSON 文件的內(nèi)容并將信息保存在字典中。我有以下問題:如何迭代 ['node']['edge_media_to_tagged_user']['edges'] 中的對象數(shù)組?這是我的代碼:info = {}def store_data(file): with open(file) as f: data = json.load(f) try: info["tagged_users"] = data['node']['edge_media_to_tagged_user']['edges'][0]['node']['user']['username'] except KeyError: info["tagged_users"] = "null" return info如下圖所示,數(shù)組中有 12 個對象,我的代碼僅適用于第一個 [0]。我需要獲取每個人的用戶名。數(shù)組示例基本上:data['node']['edge_media_to_tagged_user']['edges'][0]['node']['user']['username']data['node']['edge_media_to_tagged_user']['edges'][1]['node']['user']['username']data['node']['edge_media_to_tagged_user']['edges'][2]['node']['user']['username']data['node']['edge_media_to_tagged_user']['edges'][3]['node']['user']['username']...data['node']['edge_media_to_tagged_user']['edges'][x]['node']['user']['username']
1 回答

交互式愛情
TA貢獻(xiàn)1712條經(jīng)驗 獲得超3個贊
當(dāng)然,您的代碼僅適用于一個邊緣 - 這就是您所要求的
下面data['node']['edge_media_to_tagged_user']['edges']
是字典列表。
要迭代列表,您只需使用 for 循環(huán)
for each_edge in data['node']['edge_media_to_tagged_user']['edges']: print(each_edge) # or whatever you wish to do here
添加回答
舉報
0/150
提交
取消