Python将字典视为字符串

时间:2015-10-27 16:56:22

标签: python string dictionary

我正在处理一个项目,我需要从字典中的特定键中获取值。但是,当我尝试访问该密钥时,我收到此错误:

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将字典视为字符串吗?

按要求打印的结果(类型(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'

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

为了缩小问题的范围,我建议你验证它是否是一个有效的字典。为此,请使用以下

  

https://pypi.python.org/pypi/voluptuous

如果您真的指向字典或其他内容,那么它会指出。

相关问题