切换字典的值不能正常工作。

问题描述 投票:0回答:1

所以我有一个json文件,其中包含角色id(一个名为role的字典),有一个key(就是角色的名字),每个key都有角色的id作为它的值。

看起来像这样。

{"Ban": ["694175790639743076"], "Muted": ["692863646543380590"]}

我主要是需要json里的IDs。

使用类似 roles['Muted']roles.get('Muted') 给我 ['692863646543380590']:Muted=roles.get('Muted')print(Muted)

函数取整数,所以我必须去掉 [' 的输出,并得到这个。692863646543380590

切片只是给我 [] 无论我用什么切片。work=boost1[2:20] *or any other slice*print(work)给出 "[]"

为什么slicing在这里就不能用了呢?

python json dictionary slice discord.py
1个回答
0
投票

首先 roles['Muted'] 是一个列表,如果你只想要第一个元素,那么就用 0 索引,然后尝试使用 int() 用于将其转换为Integer。

Muted=int(roles.get('Muted')[0]) # or Muted=int(roles['Muted'][0])

静音就可以了

692863646543380590

0
投票

试试这个

work = int(boost1[0]) #fetching the first element of list and converting it into int
© www.soinside.com 2019 - 2024. All rights reserved.