为什么它只返回json的值而不是key?

ar7v8xwq  于 2023-04-08  发布在  其他
关注(0)|答案(1)|浏览(124)

此问题已在此处有答案

Iterating over dictionaries using 'for' loops(16个回答)
6天前关闭
我想检查添加的React(不一致)是否在正确的通道和正确的消息上。我将通道ID和消息ID存储在.json文件中,但python不想发送密钥的值,我不知道如何解决这个问题
{"931966019612864623": "931966020808228922"}〈==这是我的json文件
931966020808228922〈==这是python返回的
这是我的代码

elif payload.emoji.name == '🙋':
            
            if payload.user_id != self.client.user.id:
                count = 0
                with open(ticket_msg, 'r') as f:
                    distros_dict = json.load(f)
                    await channell.send(distros_dict)

                for key in distros_dict.keys():
                    channel_id = distros_dict.get(key)
                    await channell.send(str(channell.id) + ' ' + str(channel_id))

                    if int(channell.id) == int(channel_id):
                        count+=1

                if count > 0:

                    await channell.send("ok")
                else:
                    return

我已经测试过用for key in distros_dict.items()替换for key in distros_dict.keys(),但它返回了以下错误:

Ignoring exception in on_raw_reaction_add
Traceback (most recent call last):
  File "C:\Users\baron_btjit4i\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 343, in _run_event
    await coro(*args, **kwargs)
  File "c:\Users\baron_btjit4i\Desktop\Autrebot\Cogs\ticket.py", line 170, in on_raw_reaction_add
    if int(channell.id) == int(channel_id):
TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'
aor9mmx1

aor9mmx11#

使用

for key, value in distros_dict.items()

我更新了

elif payload.emoji.name == '🙋':
           
           if payload.user_id != self.client.user.id:
               count = 0
               with open(ticket_msg, 'r') as f:
                   distros_dict = json.load(f)
                   await channell.send(distros_dict)

               for key, value in distros_dict.items():
                   channel_id = key
                   await channell.send(str(channell.id) + ' ' + str(channel_id))

                   if int(channell.id) == int(channel_id):
                       count+=1

               if count > 0:

                   await channell.send("ok")
               else:
                   return

相关问题