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

Python 如果满足要求,如何从json获取2个密钥,python,json,Python,Json,我正在做一个项目,该项目列出了Twitch上最热门的5条实时地雷船拖缆。要创建此列表,我想从状态为“skyblock”或“Moded”且broadcast\u platform为live的流中获取显示名称和查看器。但我还没弄明白怎么做。我是python新手,所以如果真的很容易,请不要烤我;)。提前谢谢你的帮助 我试过使用 name=[x['display\u name'] 对于数据['streams'][0]['channel']中的x 如果有(在x.get('status','').lower

我正在做一个项目,该项目列出了Twitch上最热门的5条实时地雷船拖缆。要创建此列表,我想从状态为“skyblock”或“Moded”且
broadcast\u platform
live
的流中获取显示名称和查看器。但我还没弄明白怎么做。我是python新手,所以如果真的很容易,请不要烤我;)。提前谢谢你的帮助

我试过使用

name=[x['display\u name']
对于数据['streams'][0]['channel']中的x
如果有(在x.get('status','').lower()中表示在['skyblock','moded']中的)
]

但这会产生以下错误:
AttributeError:“str”对象没有属性“get”
,我不知道如何添加一种方法来检查
broadcast\u 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."
     }
  },

要根据通道状态获取显示名称,请执行以下操作:

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']

欢迎来到StackOverflow。很高兴你用代码编辑了你的问题,但是。。。python有非常具体的格式规则。如果没有正确格式化您发布的代码,就不可能阅读它。请再次编辑以正确的缩进格式。很抱歉,根据我对您问题的理解,我在@DavidMakogonHi-Oh再次编辑了我的帖子:name=[x['channel']['display_name']代表数据['streams']中的x(如果有)(x['channel'].get('status','')。lower代表['skyblock','moded']中的s)@Anup感谢您解决了我的第一个问题,但我也在努力确保“广播平台”:“直播”。你知道我该怎么做吗?@ohhey你的意思是:在数据['streams']中为x命名['x['channel']['display_name'],如果有的话(在x['channel']中)。获取('status','')。在['skyblock','moded']中为s降下())和x['broadcast_platform']='live']