[英]json.load agony to create dictionary instead of list
I have a python script which grabs data from a site in json: 我有一个python脚本,可从json中的站点获取数据:
channels_json = json.loads(url)
the site returns data as follows: 该网站返回的数据如下:
[ { '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' },
....]
The problem is that Python is making this into a list rather than a dictionary. 问题在于Python将其放入列表而不是字典中。 So I can't reference '4' like this:
所以我不能这样引用“ 4”:
print (channels_json["4"])
and get the response: 并得到响应:
http://ht.co/56d3a01f6e5e72cb5840556d80a52adf2871d
Instead Python spits out: 相反,Python吐出:
TypeError: list indices must be integers, not str
If I run this code: 如果我运行此代码:
for c in channels_json:
print c
Python prints out each set of coupled data like this: 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' },
How can I get the above into a dictionary so I can reference value '6' as a string and get back 如何将以上内容放入字典,以便可以将值“ 6”作为字符串引用并返回
http://ht.co/9ed0bb4cc44bb99c9ce609916ccf931f16a
You can create the dictionary you want by iterating over the dictionaries in the list
returned from json.load()
as shown below: 您可以通过遍历从
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.
Output: 输出:
{
"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"
}
dd = {}
for d in c:
for key, value in d.items():
dd[key] = value
You can just iterate through the array and build your dictionary 您可以遍历数组并构建字典
channels_json = {}
channels_array = json.loads(url)
for d in channels_array:
key = list(d.keys())[0]
val = d[key]
channels_json[key] = val
Now you should be able to reference your dictionary channels_json
现在,您应该能够引用您的字典
channels_json
More pythonic way of doing this. 这样做的更多pythonic方法。
channels = {}
for i in json.loads(url): channels.update(i)
or 要么
channels = {}
[channels.update(i) for i in json.loads(url)]
In both cases the dictionary gets updated with your list of separate dictionaries. 在这两种情况下,词典都会使用您的单独词典列表进行更新。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.