如果满足要求,如何从json获得2个密钥

时间:2018-01-06 14:33:14

标签: python json

我正在开展一个项目,该项目列出了Twitch上排名前5的现场排雷。要创建此列表,我希望从状态为“天空块”或“broadcast_platform”的流中获取display_names和viewers,livenames = [x['display_name'] for x in data['streams'][0]['channel'] if any(s in x.get('status', '').lower() for s in ['skyblock', 'modded']) ]。但我还没有弄清楚如何做到这一点。我是蟒蛇新手,所以如果真的很容易请不要烤我;)。在此先感谢您的帮助

我尝试过使用

AttributeError: 'str' object has no attribute 'get'

但是这会出现以下错误:broadcast_platform并且我不知道如何添加一种方法来检查live'_total':115, 'streams':[ { '_id':27188411728, 'game':'Minecraft', 'broadcast_platform':'live', 'community_id':'227ee534-e395-4c02-b7e9-cc0160a7159c', 'community_ids':[ '227ee534-e395-4c02-b7e9-cc0160a7159c' ], 'viewers':503, 'video_height':720, 'average_fps':60.0220264317, 'delay':0, 'created_at':'2018-01-06T12:42:27Z', 'is_playlist':False, 'stream_type':'live', 'preview':{ 'small':'https://static-cdn.jtvnw.net/previews-ttv/live_user_lolddog-80x45.jpg', 'medium':'https://static-cdn.jtvnw.net/previews-ttv/live_user_lolddog-320x180.jpg', 'large':'https://static-cdn.jtvnw.net/previews-ttv/live_user_lolddog-640x360.jpg', 'template':'https://static-cdn.jtvnw.net/previews-ttv/live_user_lolddog-{width}x{height}.jpg' }, 'channel':{ 'mature':False, 'status':'잉여맨 생방송 개꿀잼 아오오니 컨텐츠 (minecraft)', 'broadcaster_language':'en', 'display_name':'ㅁ잉여맨', 'game':'Minecraft', 'language':'ko', '_id':145971530, 'name':'lolddog', 'created_at':'2017-01-25T19:06:37.322554Z', 'updated_at':'2018-01-06T14:06:35.661381Z', 'partner':False, 'logo':'https://static-cdn.jtvnw.net/jtv_user_pictures/a7439e1e92bae2cb-profile_image-300x300.png', 'video_banner':'https://static-cdn.jtvnw.net/jtv_user_pictures/12d86926b1ed139e-channel_offline_image-1920x1080.png', 'profile_banner':'https://static-cdn.jtvnw.net/jtv_user_pictures/lolddog-profile_banner-51fc562927fcd0a9-480.png', 'profile_banner_background_color':'', 'url':'https://www.twitch.tv/lolddog', 'views':64203, 'followers':6191, 'broadcaster_type':'', 'description':'잉여맨의 마인크래프트 신선하고 유쾌한 방송 입니다.' } }, { '_id':27187856336, 'game':'Minecraft', 'broadcast_platform':'live', 'community_id':'0b9d06f9-fe5f-4f69-a4c3-2a9ee5604fb7', 'community_ids':[ '0b9d06f9-fe5f-4f69-a4c3-2a9ee5604fb7', '227ee534-e395-4c02-b7e9-cc0160a7159c', 'fd0eab99-832a-4d7e-8cc0-04d73deb2e54' ], 'viewers':422, 'video_height':900, 'average_fps':60, 'delay':0, 'created_at':'2018-01-06T11:00:20Z', 'is_playlist':False, 'stream_type':'live', 'preview':{ 'small':'https://static-cdn.jtvnw.net/previews-ttv/live_user_matrixis-80x45.jpg', 'medium':'https://static-cdn.jtvnw.net/previews-ttv/live_user_matrixis-320x180.jpg', 'large':'https://static-cdn.jtvnw.net/previews-ttv/live_user_matrixis-640x360.jpg', 'template':'https://static-cdn.jtvnw.net/previews-ttv/live_user_matrixis-{width}x{height}.jpg' }, 'channel':{ 'mature':True, 'status':'Weekend derping !prime - Modern Skyblock 2', 'broadcaster_language':'en', 'display_name':'Matrixis', 'game':'Minecraft', 'language':'en', '_id':32776386, 'name':'matrixis', 'created_at':'2012-08-06T17:03:31.398564Z', 'updated_at':'2018-01-06T14:05:54.989584Z', 'partner':True, 'logo':'https://static-cdn.jtvnw.net/jtv_user_pictures/matrixis-profile_image-526891ecc78e1d4a-300x300.png', 'video_banner':'https://static-cdn.jtvnw.net/jtv_user_pictures/matrixis-channel_offline_image-efea8977a5d238db-1920x1080.png', 'profile_banner':'https://static-cdn.jtvnw.net/jtv_user_pictures/matrixis-profile_banner-3eb2813de7a4bfdb-480.png', 'profile_banner_background_color':'', 'url':'https://www.twitch.tv/matrixis', 'views':1507379, 'followers':77924, 'broadcaster_type':'', 'description':"I'm a Geek through and through. \rI'll talk about anything technical. \rComputer Engineer by trade. \rGamer for Life." } },

Linear Layout

1 个答案:

答案 0 :(得分:0)

根据频道状态获取显示名称:

names = [x['channel']['display_name'] for x in data['streams'] if any(s in x['channel'].get('status', '').lower() for s in ['skyblock', 'modded'])] 

根据频道状态和广播平台获取显示名称:

names = [x['channel']['display_name'] for x in data['streams'] if any(s in x['channel'].get('status', '').lower() for s in ['skyblock', 'modded']) and x['broadcast_platform'] == 'live']