[英]JSON in python discord.py
我正在学习对 discord 机器人进行编码,我正在尝试添加一个“金钱”function,每次他发送消息时都会向用户“帐户”添加“金钱”。 这是我的问题,json 文件写入 'id': 0 但 python 文件写入 id: 0
from discord.ext import commands
import json
with open('money.json', 'r') as infile:
argent = json.load(money)
print(money)
@bot.event
async def on_message(message):
if not message.author.bot:
id = message.author.id
if id not in money:
argent[id] = 0.00
argent[id] += 5.00
print(money)
with open('money.json', 'w+') as outfile:
json.dump(argent, outfile)
Here's my problem, when python load the JSON file, it creates python dict('id': value) but when python write in the dict, it writes dict(id: value)
从 JSON 加载的字典:
{'299561981311057920': 5.0}
程序在其上写入后的相同字典:
{'299561981311057920': 5.0, 299561981311057920: 5.0}
我希望它写成:
{'299561981311057920': 10.0}
将您的 id 转换为字符串,然后再从您的 dict 中提取它
id = str(message.author.id)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.