我正在處理一個項目,其中需要從字典中的特定鍵獲取值。然而,當我嘗試訪問該密鑰我得到這個錯誤:Python將字典視爲字符串
TypeError: string indices must be integers
我打開這個樣子的,當我運行的詞典:打印(TWIT):
{"in_reply_to_status_id": 659033881912344576, "text": "@IncisiveDame I'm getting it on Friday when it's out in the UK", "favorited": false, "lang": "en", "id": 659034005430517760, "is_quote_status": false, "favorite_count": 0, "user": {"location": "Cuteville", "is_translator": false, "id": 2345667376, "has_extended_profile": false, "profile_background_color": "C0DEED", "default_profile_image": false, "followers_count": 1042, "following": false, "lang": "en", "profile_sidebar_border_color": "C0DEED", "notifications": false, "favourites_count": 4621, "is_translation_enabled": false, "listed_count": 6, "created_at": "Sat Feb 15 21:51:33 +0000 2014", "profile_use_background_image": true, "default_profile": true, "statuses_count": 15909, "id_str": "2345667376", "verified": false, "profile_link_color": "0084B4", "utc_offset": null, "contributors_enabled": false, "entities": {"url": {"urls": [{"url": "a://t.co/gLa4g7Bruy", "expanded_url": "a://instagram.com/georgedriver987", "indices": [0, 22], "display_url": "instagram/georgedriver987"}]}, "description": {"urls": []}}, "profile_background_tile": false, "screen_name": "PaigeBeckyGuy", "profile_banner_url": "a://pbs.twimg.com/profile_banners/2345667376/1445165054", "profile_image_url_https": "a://pbs.twimg.com/profile_images/640439872684204032/iq5ugyGA_normal.jpg", "profile_sidebar_fill_color": "DDEEF6", "description": "17 - Massive Paige and Becky Lynch fan #FreaksAndGeeks", "profile_text_color": "333333", "geo_enabled": true, "profile_image_url": "a://pbs.twimg.com/profile_images/640439872684204032/iq5ugyGA_normal.jpg", "profile_background_image_url": "a://abs.twimg.com/images/themes/theme1/bg.png", "friends_count": 158, "url": "a://t.co/gLa4g7Bruy", "protected": false, "profile_background_image_url_https": "a://abs.twimg.com/images/themes/theme1/bg.png", "name": "George", "time_zone": null, "follow_request_sent": false}, "created_at": "Tue Oct 27 15:48:45 +0000 2015", "geo": null, "retweeted": false, "coordinates": null, "id_str": "659034005430517760", "retweet_count": 0, "metadata": {"result_type": "recent", "iso_language_code": "en"}, "truncated": false, "entities": {"symbols": [], "user_mentions": [{"screen_name": "IncisiveDame", "id": 2504947580, "name": "Imigie.", "id_str": "2504947580", "indices": [0, 13]}], "urls": [], "hashtags": []}, "in_reply_to_screen_name": "IncisiveDame", "in_reply_to_status_id_str": "659033881912344576", "contributors": null, "place": null, "source": "<a href=\"a://twitter.com/#!/download/ipad\" rel=\"nofollow\">Twitter for iPad</a>", "in_reply_to_user_id": 2504947580, "in_reply_to_user_id_str": "2504947580"}
代碼用來獲取鍵(twit是字典):
print(twit['id'])
有人可以向我解釋爲什麼Python將字典視爲字符串?按要求打印(類型(蠢),再版(蠢))的
結果:
<class 'str'> '{"in_reply_to_status_id": 659033881912344576, "text": "@IncisiveDame I\'m getting it on Friday when it\'s out in the UK", "favorited": false, "lang": "en", "id": 659034005430517760, "is_quote_status": false, "favorite_count": 0, "user": {"is_translator": false, "id": 2345667376, "has_extended_profile": false, "profile_background_color": "C0DEED", "default_profile_image": false, "followers_count": 1042, "following": false, "lang": "en", "profile_sidebar_border_color": "C0DEED", "notifications": false, "favourites_count": 4621, "is_translation_enabled": false, "listed_count": 6, "created_at": "Sat Feb 15 21:51:33 +0000 2014", "profile_use_background_image": true, "default_profile": true, "statuses_count": 15909, "id_str": "2345667376", "follow_request_sent": false, "verified": false, "profile_link_color": "0084B4", "screen_name": "PaigeBeckyGuy", "utc_offset": null, "contributors_enabled": false, "entities": {"url": {"urls": [{"url": "http://t.co/gLa4g7Bruy", "expanded_url": "http://instagram.com/georgedriver987", "indices": [0, 22], "display_url": "instagram.com/georgedriver987"}]}, "description": {"urls": []}}, "profile_background_tile": false, "profile_banner_url": "https://pbs.twimg.com/profile_banners/2345667376/1445165054", "profile_image_url_https": "https://pbs.twimg.com/profile_images/640439872684204032/iq5ugyGA_normal.jpg", "profile_sidebar_fill_color": "DDEEF6", "description": "17 - Massive Paige and Becky Lynch fan #FreaksAndGeeks", "profile_text_color": "333333", "geo_enabled": true, "location": "Cuteville", "profile_background_image_url": "http://abs.twimg.com/images/themes/theme1/bg.png", "friends_count": 158, "url": "http://t.co/gLa4g7Bruy", "protected": false, "profile_background_image_url_https": "https://abs.twimg.com/images/themes/theme1/bg.png", "name": "George", "time_zone": null, "profile_image_url": "http://pbs.twimg.com/profile_images/640439872684204032/iq5ugyGA_normal.jpg"}, "created_at": "Tue Oct 27 15:48:45 +0000 2015", "geo": null, "retweeted": false, "coordinates": null, "id_str": "659034005430517760", "retweet_count": 0, "metadata": {"result_type": "recent", "iso_language_code": "en"}, "truncated": false, "entities": {"symbols": [], "user_mentions": [{"screen_name": "IncisiveDame", "id": 2504947580, "name": "Imigie.", "id_str": "2504947580", "indices": [0, 13]}], "urls": [], "hashtags": []}, "in_reply_to_screen_name": "IncisiveDame", "in_reply_to_status_id_str": "659033881912344576", "contributors": null, "place": null, "source": "<a href=\\"http://twitter.com/#!/download/ipad\\" rel=\\"nofollow\\">Twitter for iPad</a>", "in_reply_to_user_id": 2504947580, "in_reply_to_user_id_str": "2504947580"}\n'
那麼,顯然'twit'不是字典。你需要顯示更多的代碼。 –
調試時,請首先到達'print'。請編輯您的問題,以便在您的print(twid [「id」])'行之前立即包含'print(type(twit),repr(twit))'的結果。 – DSM