【问题标题】:Cannot find variables in a list of dictionaries在字典列表中找不到变量
【发布时间】:2017-11-01 22:14:06
【问题描述】:

我一般都在尝试使用 API。为了测试这一点,我编写了这个小 sn-p 代码以获得瑞典国家公共服务电台所有频道的列表,我想打印频道的 ID 和名称:

import requests as rq
import json
from pprint import pprint

resp = rq.get('http://api.sr.se/api/v2/channels?
    format=json&indent=TRUE')

respjson = json.loads(resp.text)

pprint (respjson['id'])

我得到了错误

File "sr-api.py", line 9, in <module>
pprint (respjson['id']['name'])
KeyError: 'id'

(缩写)“respjson”看起来像这样

{'channels': [{'channeltype': 'Rikskanal',
           'color': '31a1bd',
           'id': 132,
           'image': 'http://static-cdn.sr.se/sida/images/132/2186745_512_512.jpg?preset=api-default-square',
           'imagetemplate': 'http://static-cdn.sr.se/sida/images/132/2186745_512_512.jpg',
           'liveaudio': {'id': 132,
                         'statkey': '/app/direkt/p1[k(132)]',
                         'url': 'http://sverigesradio.se/topsy/direkt/srapi/132.mp3'},
           'name': 'P1',
           'scheduleurl': 'http://api.sr.se/v2/scheduledepisodes?channelid=132',
           'siteurl': 'http://sverigesradio.se/p1',
           'xmltvid': 'p1.sr.se'},
{'channeltype': 'Lokal kanal',
           'color': 'c31eaa',
           'id': 200,
           'image': 'http://static-cdn.sr.se/sida/images/200/2186775_512_512.jpg?preset=api-default-square',
           'imagetemplate': 'http://static-cdn.sr.se/sida/images/200/2186775_512_512.jpg',
           'liveaudio': {'id': 200,
                         'statkey': '/app/direkt/p4 jämtland[k(200)]',
                         'url': 'http://sverigesradio.se/topsy/direkt/srapi/200.mp3'},
           'name': 'P4 Jämtland',
           'scheduleurl': 'http://api.sr.se/v2/scheduledepisodes?channelid=200',
           'siteurl': 'http://sverigesradio.se/jamtland/',
           'xmltvid': 'p4jmtl.sr.se'}],
'copyright': 'Copyright Sveriges Radio 2017. All rights reserved.',
'pagination': {'nextpage': 'http://api.sr.se/v2/channelsformat=json&indent=true&page=2',
            'page': 1,
            'size': 10,
            'totalhits': 55,
            'totalpages': 6}}

【问题讨论】:

  • 是的。你为什么加急['id'] 上班?漂亮的打印输出清楚地表明顶层没有这样的键......所以你必须先做['channels'],它会给你一个列表,其中包含更多dict对象,然后对于列表中的每个dict 对象,您需要访问'id'。
  • 因为我不知道更好:-)

标签: python json python-3.x api pprint


【解决方案1】:

频道是一个列表。您必须对其进行迭代以获取所有频道并打印它们的 ID。

# starting from respjson
respjson = {
    'channels': [
        {
            'channeltype': 'Rikskanal',
            'color': '31a1bd',
            'id': 132,
            'image': 'http://static-cdn.sr.se/sida/images/132/2186745_512_512.jpg?preset=api-default-square',
            'imagetemplate': 'http://static-cdn.sr.se/sida/images/132/2186745_512_512.jpg',
            'liveaudio': {'id': 132,
                          'statkey': '/app/direkt/p1[k(132)]',
                          'url': 'http://sverigesradio.se/topsy/direkt/srapi/132.mp3'},
            'name': 'P1',
            'scheduleurl': 'http://api.sr.se/v2/scheduledepisodes?channelid=132',
            'siteurl': 'http://sverigesradio.se/p1',
            'xmltvid': 'p1.sr.se'},
        {
            'channeltype': 'Lokal kanal',
            'color': 'c31eaa',
            'id': 200,
            'image': 'http://static-cdn.sr.se/sida/images/200/2186775_512_512.jpg?preset=api-default-square',
            'imagetemplate': 'http://static-cdn.sr.se/sida/images/200/2186775_512_512.jpg',
            'liveaudio': {'id': 200,
                          'statkey': '/app/direkt/p4 jämtland[k(200)]',
                          'url': 'http://sverigesradio.se/topsy/direkt/srapi/200.mp3'},
            'name': 'P4 Jämtland',
            'scheduleurl': 'http://api.sr.se/v2/scheduledepisodes?channelid=200',
            'siteurl': 'http://sverigesradio.se/jamtland/',
            'xmltvid': 'p4jmtl.sr.se'
        }
    ],
    'copyright': 'Copyright Sveriges Radio 2017. All rights reserved.',
    'pagination': {
        'nextpage': 'http://api.sr.se/v2/channelsformat=json&indent=true&page=2',
        'page': 1,
        'size': 10,
        'totalhits': 55,
        'totalpages': 6
    }
}

for channel in respjson['channels']:
    print(channel['id'])

【讨论】:

    【解决方案2】:

    您要做的是查看频道内提供的字典,您可以使用以下内容...

    for dic in respjson['channels']:
        pprint(dic['id'])
    

    【讨论】:

    • 知道了!非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2012-08-07
    • 2016-06-07
    • 1970-01-01
    • 2018-10-27
    • 2016-08-05
    • 1970-01-01
    • 1970-01-01
    • 2021-12-14
    相关资源
    最近更新 更多