首页 > 解决方案 > 字典更新序列元素#0的长度为1;2 是必需的错误 | Python

问题描述

尝试从字典中获取信息时出现此错误。

#1。这个错误是什么意思?#2。我将如何解决它

这是我当前的代码:

    getGroup = requests.get(url = "https://groups.roblox.com/v2/groups?groupIds=" + str(groupId))
    req = getGroup.text
    print(dict(req)["data"][0])

引发的错误:

Command raised an exception: ValueError: dictionary update sequence element #0 has length 1; 2 is required

请求获得的信息:

{'data': [{'id': 7850173, 'name': 'Project Studios - Roblox', 'description': 'Happy May! \n\nJoin our Communications server to see the progress on our latest projects. \n\nCheck out my friends groups in the Affiliates tab! \n\nFunny Fortnite?\n\n- Group Description Updated 5-1-21 -', 'owner': {'id': 55065985, 'type': 'User'}, 'memberCount': 428, 'created': '2020-09-24T00:01:23.853Z'}]}

如果您能提供帮助,非常感谢!

标签: python

解决方案


您需要先将 json 响应转换为数据结构。

getGroup = requests.get(url = "https://groups.roblox.com/v2/groups?groupIds=" + str(groupId))
req = getGroup.json()
await ctx.send(dict(req)["data"][0])

推荐阅读