【发布时间】:2016-01-27 05:16:02
【问题描述】:
我正在做一个项目,我需要从字典中的特定键中获取值。但是,当我尝试访问该密钥时,出现此错误:
TypeError: string indices must be integers
我运行时打开的字典是这样的:print(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 将字典视为字符串吗?
按要求打印(type(twit), repr(twit)) 的结果:
<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))的结果。
标签: python string dictionary