【问题标题】:json.load agony to create dictionary instead of listjson.load 痛苦创建字典而不是列表
【发布时间】:2019-01-14 16:06:05
【问题描述】:

我有一个 python 脚本,它从 json 中的站点获取数据:

channels_json = json.loads(url)

网站返回数据如下:

[ { '1': 'http://ht.co/bbda24210d7bgfbbbbcdfc2a023f' },
{ '2': 'http://ht.co/bbd10d7937932965369c248f7ccdfc2a023f' },
{ '3': 'http://ht.co/d3a01f6e5e74eb2cb5840556d80a52adf2871d' },
{ '4': 'http://ht.co/56d3a01f6e5e72cb5840556d80a52adf2871d' },
{ '5': 'http://ht.co/9ed0bb4cc447b99c9ce609916ccf931f16a' },
{ '6': 'http://ht.co/9ed0bb4cc44bb99c9ce609916ccf931f16a' },
....]

问题在于 Python 将它变成了一个列表而不是字典。所以我不能像这样引用“4”:

print (channels_json["4"])

并得到响应:

http://ht.co/56d3a01f6e5e72cb5840556d80a52adf2871d    

相反,Python 吐了出来:

TypeError: list indices must be integers, not str

如果我运行这段代码:

for c in channels_json:
   print c

Python 像这样打印出每组耦合数据:

{u'1': u'http://ht.co/bbda24210d7bgfbbbbcdfc2a023f' },
{ u'2': u'http://ht.co/bbd10d7937932965369c248f7ccdfc2a023f' },
{ u'3': u'http://ht.co/d3a01f6e5e74eb2cb5840556d80a52adf2871d' },
{ u'4': u'http://ht.co/56d3a01f6e5e72cb5840556d80a52adf2871d' },
{ u'5': u'http://ht.co/9ed0bb4cc447b99c9ce609916ccf931f16a' },
{ u'6': u'http://ht.co/9ed0bb4cc44bb99c9ce609916ccf931f16a' },

如何将上述内容放入字典中,以便将值“6”作为字符串引用并返回

http://ht.co/9ed0bb4cc44bb99c9ce609916ccf931f16a

【问题讨论】:

  • 你能展示你的尝试吗?似乎是迭代列表并将内部字典一次添加到单个字典的简单案例。
  • 您收到错误的原因(在我看来)是您尝试使用数组(列表)中的键访问元素,这是不可能的,请尝试执行 my_link = link[4 ] 然后 my_link["5"] (数组从 0 开始!!)
  • 如果您的列表仅包含一项,请使用 channels_json = json.loads(url)[0]
  • 每次我尝试通过引用数组中的值(如下所示)从列表中获取值时,我都会得到value NameError: name 'value' is not defined

标签: python json string list dictionary


【解决方案1】:

您可以通过迭代从json.load() 返回的list 中的字典来创建您想要的字典,如下所示:

#json_data = json.loads(url)

json_data = [{ '1': 'http://ht.co/bbda24210d7bgfbbbbcdfc2a023f' },
             { '2': 'http://ht.co/bbd10d7937932965369c248f7ccdfc2a023f' },
             { '3': 'http://ht.co/d3a01f6e5e74eb2cb5840556d80a52adf2871d' },
             { '4': 'http://ht.co/56d3a01f6e5e72cb5840556d80a52adf2871d' },
             { '5': 'http://ht.co/9ed0bb4cc447b99c9ce609916ccf931f16a' },
             { '6': 'http://ht.co/9ed0bb4cc44bb99c9ce609916ccf931f16a' },]

# Convert to single dictionary.    
channels_json = dict(d.popitem() for d in json_data)

print(json.dumps(channels_json, indent=4))  # Pretty-print result.

输出:

{
    "1": "http://ht.co/bbda24210d7bgfbbbbcdfc2a023f",
    "2": "http://ht.co/bbd10d7937932965369c248f7ccdfc2a023f",
    "3": "http://ht.co/d3a01f6e5e74eb2cb5840556d80a52adf2871d",
    "4": "http://ht.co/56d3a01f6e5e72cb5840556d80a52adf2871d",
    "5": "http://ht.co/9ed0bb4cc447b99c9ce609916ccf931f16a",
    "6": "http://ht.co/9ed0bb4cc44bb99c9ce609916ccf931f16a"
}

【讨论】:

    【解决方案2】:
     dd = {}
    
     for d in c:
         for key, value in d.items():
             dd[key] = value
    

    【讨论】:

    • 越来越近,但这吐了for key, value in d.items(): AttributeError: 'unicode' object has no attribute 'items'
    【解决方案3】:

    您可以遍历数组并构建您的字典

    channels_json = {}
    channels_array = json.loads(url)
    for d in channels_array:
        key = list(d.keys())[0]
        val = d[key]
        channels_json[key] = val
    

    现在您应该可以参考您的字典channels_json

    【讨论】:

    • 我只是好奇...这怎么行? d.keys() 有什么好处???
    • d.keys() 返回字典中的所有键。因此,如果您只有 1 个键,则只需选择第 0 个索引
    • 感谢澄清它^^
    • 感谢您的回复,但这只会引发以下错误:channels_json[key] = value NameError: name 'value' is not defined
    • @manners 你遇到了什么错误?我尝试过这个。效果很好
    【解决方案4】:

    这样做的更多pythonic方式。

    channels = {}
    for i in json.loads(url): channels.update(i)
    

    channels = {}
    [channels.update(i) for i in json.loads(url)]
    

    在这两种情况下,字典都会根据您的单独字典列表进行更新。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-03-12
      • 1970-01-01
      • 1970-01-01
      • 2018-09-06
      • 1970-01-01
      • 2012-04-21
      • 1970-01-01
      相关资源
      最近更新 更多