Python将字典视为字符串

Python将字典视为字符串,python,string,dictionary,Python,String,Dictionary,我正在做一个项目,在这个项目中,我需要从字典中的特定键获取值。但是,当我尝试访问该密钥时,会出现以下错误: TypeError: string indices must be integers 我打开的词典在运行时如下所示:print(twit): 有人能给我解释一下为什么Python将字典视为字符串吗 按要求打印结果(类型(twit)、报告(twit)): <class 'str'> '{"in_reply_to_status_id": 659033881912344576, "

我正在做一个项目,在这个项目中,我需要从字典中的特定键获取值。但是,当我尝试访问该密钥时,会出现以下错误:

TypeError: string indices must be integers
我打开的词典在运行时如下所示:print(twit):

有人能给我解释一下为什么Python将字典视为字符串吗

按要求打印结果(类型(twit)、报告(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'
“{”在对状态id的回复中:659033881912344576,“文本”:“@IncisiveDame我周五在英国发布时收到”,“favorited”:false,“lang”:“en”,“id”:659034005430517760,“是状态”:false,“最喜欢的计数”:0,“用户”:{“是翻译”:false,“id”:2345667376,“是否扩展了配置文件”:false,“配置文件背景颜色”:“c0dect”,“default\u profile\u image”:false,“followers\u count”:1042,“following”:false,“lang”:“en”,“profile\u sidebar\u border\u color”:“c0dect”,“notifications”:false,“favorites\u count”:4621,“false\u translation\u enabled”:false,“listed\u count”:6,“created\u at”:“Sat Feb 15 21:51:33+0000 2014”,“profile\u use\u background\u image”:true,“default\u profile\u profile\u“: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,“entications”:{“url”:{“url”:[{“url”:”http://t.co/gLa4g7Bruy,“扩展的url”:http://instagram.com/georgedriver987", “索引:[0,22],“显示url”:“instagram.com/georgedriver987”}],“说明”:{“url”:[]}},,“配置文件背景图”:false,“配置文件旗帜图”:”https://pbs.twimg.com/profile_banners/2345667376/1445165054,“配置文件\u图像\u url\u https”:https://pbs.twimg.com/profile_images/640439872684204032/iq5ugyGA_normal.jpg", "配置文件侧栏填充颜色“:“DDEEF6”,“描述“:“17-大量佩奇和贝基·林奇粉丝#怪胎和极客”,“配置文件文本颜色“:“333333”,“地理位置启用”:真,“位置“:“Cuteville”,“配置文件背景图片url”:”http://abs.twimg.com/images/themes/theme1/bg.png,“好友数”:158,“url”:http://t.co/gLa4g7Bruy,“受保护”:false配置文件\u背景\u图像\u url\u https:“https://abs.twimg.com/images/themes/theme1/bg.png“,”名称“:”乔治“,”时区“:”空,“配置文件\图像\ url”:”http://pbs.twimg.com/profile_images/640439872684204032/iq5ugyGA_normal.jpg},“创建于”:“2015年10月27日星期二15:48:45+0000”,“地理位置”:空,“转发”:假,“坐标”:空,“id_str”:659034005430517760,“转发计数”:0,“元数据”:{“结果类型”:“最近”,“iso语言代码”:“en”},“截断”:假,“实体”:{“符号”:[],“用户提及”:[{“屏幕名称”:“切割数据”,“id”:2504947580,“名称”:“Imigie.”,“id街”:“2504947580”,“索引”:[0,13],“URL”:“,”标签[],”在回复屏幕名称时:IncisiveDame、in-reply to-status、in-reply to-user id、in-reply to-user id、in-reply to-user-id、str:“659033881912344576”、“contributors:null、place:null、source:”“in-reply to-user id、in-reply to-user id、in-reply to-user-id、str:“2504947580”}\n

您似乎拥有一个字符串而不是一个字典,而且它看起来像是有效的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
>>stwit=r“{”在回复状态id时:659033881912344576,“文本”:“@IncisiveDame我周五在英国发布时收到它”,“favorited”:false,“lang”:“en”,“id”:659034005430517760,“是”状态:false,“最喜欢的计数”:0,“用户”:“{”位置“:“Cuteville”,“是”译者“:false”,“id”:234567376”,“是否扩展了个人资料”:false,“配置文件\背景\颜色”:“c0dect”,“default\u pro”
文件图片:false,“关注者数量”:1042,“跟随者数量”:false,“郎”:“恩”,“个人资料侧栏颜色”:“c0dect”,“通知”:false,“收藏夹数量”:4621,“是否启用翻译”:false,“列出数量”:6,“创建时间”:“2014年2月15日星期六21:51:33+0000”,“个人资料使用背景图片”:true,“默认个人资料”:true,“状态数量”:15909,“id_str”:“2345667376”,“已验证”:假
,“profile_link_color”:“0084B4”,“utc_offset”:null,“contributors_enabled”:false,“entities”:{“url”:{“url”:“a://t.co/gLa4g7Bruy”,“expanded_url”:“a://instagram.com/georgedriver987”,“index”:[0,22],“display_url”:“instagram/georgedriver987”},“description”:{“url”:{“url”:[]}”,profile_background_tile(false,“屏幕名称”:“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_侧边栏_fill_color:“DDEEF6”,“description:“17-大量Paige和Becky Lynch粉丝#怪人”,“profile_text_color:“333333;”,“geo_enabled”:true,“profile_image_url:”a://pbs.twimg.com/profile_images/6404398726884204032/iq5ugyGA_normal
.jpg,“profile\u background\u image\u url”:“a://abs.twimg.com/images/theme1/bg.png”,“friends\u count”:158,“url”:“a://t.co/gLa4g7Bruy”,“profile\u background\u image\u url\u https”:“a://abs.twimg.com/images/themes/theme1/bg.png”,“name”:“乔治”,“时区”:null,“follow\u request\u sent”:false,“created\u at”:2015年10月27日星期二15:48:45+0000,“地理位置”:空,“转发”:假,“首席运营官”
坐标:null,“id_str”:“659034005430517760”,“retweet_count”:0,“元数据”:{“结果类型”:“最近”,“iso_语言代码”:“en”},“截断的”:false,“实体”:{“符号”:[],“用户提及”:[{“屏幕名称”:“切割数据”,“id”:2504947580,“名称”:“Imigie.”,“id_str”:“2504947580”,“索引”:[0,13],“URL”:“hashtags”:[],”在回复到屏幕上的姓名中:“精辟的女士”,“回复到”
_状态为“659033881912344576”,“贡献者”:null,“地点”:null,“来源”:“回复用户的id:2504947580”,回复用户的id:2504947580,“回复用户的id:2504947580”}”
>>>
>>>导入json
>>>twit=json.loads(stwit)
>>>twit['id']
659034005430517760

要缩小问题范围,我建议您验证它是否是有效的词典。T
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