2017-06-12 211 views
-1

如何迭代嵌套的JSON數據,只有當鍵存在時才提取值?如何分配鍵:值字典迭代動態嵌套字典

使用Twitter API,如果tweet包含hashtag,則它嵌套在tweet - > entities - > hashtags - > 0(這是更改的位) - >文本。

如果推文的標籤超過1個,API會在內容爲0的hastags中創建一個新密鑰。因此,您可以通過tweet - > entities - > hashtags {0:foo},{1:bar },{2:SUP}等

{ "_id" : ObjectId("593adcb1b27be5eb5daa7e66"), "created_at" : "Fri Jun 09 14:54:55 +0000 2017", "id" : NumberLong(873191685915906049), "id_str" : "873191685915906049", "text" : "RT @NiamhMannion_: A beautiful day in Kinvara! Really impressed by the delicious produce @kinvaramarket #FoodieHeaven URL", "truncated" : false, "entities" : { "hashtags" : [ { "text" : "FoodieHeaven", "indices" : [ 104, 117 ] } ], "symbols" : [], "user_mentions" : [ { "screen_name" : "NiamhMannion_", "name" : "Niamh Mannion", "id" : NumberLong(2178812961), "id_str" : "2178812961", "indices" : [ 3, 17 ] }, { "screen_name" : "kinvaramarket", "name" : "KinvaraFarmersMarket", "id" : NumberLong(3064922836), "id_str" : "3064922836", "indices" : [ 89, 103 ] } ], "urls" : [] }, "source" : "<a href=\"http://twitter.com\" rel=\"nofollow\">Twitter Web Client</a>", "in_reply_to_status_id" : null, "in_reply_to_status_id_str" : null, "in_reply_to_user_id" : null, "in_reply_to_user_id_str" : null, "in_reply_to_screen_name" : null, "user" : { "id" : 37690003, "id_str" : "37690003", "name" : "Gabriela Guedez H", "screen_name" : "GabyGuedezH", "location" : "Ireland", "description" : "Award-wining food and drinks journalist at @TheTaste_ie Passionate about wines, whiskey, rum, spirits and craft beer. WSET Certified", "url" : "URL", "entities" : { "url" : { "urls" : [ { "url" : "URL", "expanded_url" : "URL", "display_url" : "URL", "indices" : [ 0, 22 ] } ] }, "description" : { "urls" : [] } }, "protected" : false, "followers_count" : 7900, "friends_count" : 5047, "listed_count" : 159, "created_at" : "Mon May 04 16:02:34 +0000 2009", "favourites_count" : 8905, "utc_offset" : 3600, "time_zone" : "Dublin", "geo_enabled" : true, "verified" : false, "statuses_count" : 12727, "lang" : "en", "contributors_enabled" : false, "is_translator" : false, "is_translation_enabled" : false, "profile_sidebar_border_color" : "5ED4DC", "profile_sidebar_fill_color" : "95E8EC", "profile_text_color" : "3C3940", "profile_use_background_image" : true, "has_extended_profile" : false, "default_profile" : false, "default_profile_image" : false, "following" : true, "follow_request_sent" : false, "notifications" : false, "translator_type" : "none" }, "geo" : null, "coordinates" : null, "place" : null, "contributors" : null, "is_quote_status" : false, "retweet_count" : 4, "favorite_count" : 14, "favorited" : true, "retweeted" : true, "possibly_sensitive" : false, "lang" : "en", "has_hashtags" : false, "is_retweet" : false }

我要採取一切現有的井號標籤,提取文本,並將其存儲在我的新字典。

for tweet in tweets: 
    thistweet = { 
     'text': tweet.text, 
     'created_at': str(tweet.created_at), 
     'retweet_count': tweet.retweet_count, 
     'favorite_count': tweet.favorite_count, 
     'geo': tweet.geo, 
     'coordinates': tweet.coordinates 
    } 
    for i in tweet[entities][hashtags][i]: 
     thistweet = thistweet.update({'hashtag'[i]: tweet.entities.hashtags.i.text}) 

這段代碼不起作用。我得到'字典'對象沒有屬性'主題標籤'。

我不完全確定如何開始嘗試解決這個問題,說實話。

+0

打印鳴叫變量的值,並檢查是否包括hashtag酒店的每實體屬性 –

+0

@ArpitSolanki始終存在hashtags.entities存在,即使沒有井號標籤,它只是空: 「entities」:{「hashtags」:[]} – Mormoran

+0

它肯定會引發關鍵錯誤。由於你的主題標籤是一個空的數組,它不會找到像i.text那樣的東西 –

回答

1

試試這個,

for tweet in tweets: 
    thistweet = { 
     'text': tweet.text, 
     'created_at': str(tweet.created_at), 
     'retweet_count': tweet.retweet_count, 
     'favorite_count': tweet.favorite_count, 
     'geo': tweet.geo, 
     'coordinates': tweet.coordinates, 
     'hashtags':{} 
    } 
    for index, item in enumerate(tweet.entities.hashtags): 
     try: 
      thistweet = thistweet['hashtags'].update({item:tweet.entities.hashtags[index]['text']}) 
     except: 
      pass 
+0

這沒有奏效,我在嘗試後得到一個錯誤:我不熟悉項目:...也許是這樣嗎? – Mormoran

+0

什麼是錯誤? – zaidfazil

+0

'tweet'['entities'] ['hashtags']'狀態'對象不能在線訂閱'「:」 – Mormoran