2015-10-27 61 views
2

我正在处理一个项目,其中需要从字典中的特定键获取值。然而,当我尝试访问该密钥我得到这个错误: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' 
+3

那么,显然'twit'不是字典。你需要显示更多的代码。 –

+1

调试时,请首先到达'print'。请编辑您的问题,以便在您的print(twid [“id”])'行之前立即包含'print(type(twit),repr(twit))'的结果。 – DSM

回答

4

你似乎有一个字符串,而不是一个字典,它看起来像有效的JSON,您可以使用json模块将字符串加载到字典中,然后从那里取出id。示例 -

import json 
twitdict = json.loads(twit) 
print(twitdict['id']) 

演示 -

>>> stwit = r"""{"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_pro 
file_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.c 
om/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, "coo 
rdinates": 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"}""" 
>>> 
>>> import json 
>>> twit = json.loads(stwit) 
>>> twit['id'] 
659034005430517760 
相关问题