【问题标题】:Delete one object from a JSON file using python使用python从JSON文件中删除一个对象
【发布时间】:2021-09-01 16:51:27
【问题描述】:
@Bot.command()
async def cl(ctx,member:discord.Member = None):
    with open("C:\python3\economy.json","r") as f:
        json.load(f)
        queue.remove(str(member.id))
        with open("C:\python3\economy.json","w") as f:
            json.dump(f)

我只需要删除图片上突出显示的文本: json file

【问题讨论】:

  • 欢迎来到 StackOverflow。您是否查看过如何从 JSON 文件中删除条目? StackOverflow 上有很多关于它的帖子。你也可以看看how to ask a good question

标签: python list discord.py


【解决方案1】:

有3个错误:

  1. 你还没有定义queue
  2. 没有dict.remove这样的方法,你在找dict.pop
  3. 您没有将任何内容保存到 JSON 文件中

修复你的代码

with open("C:\python3\economy.json", "r") as f:
    queue = json.load(f)  # defining `queue`

queue.pop(str(member.id))  # removing the key

with open("C:\python3\economy.json", "w") as f:
    json.dump(queue, f)  # saving into the JSON file

【讨论】:

  • 现在我知道了:TextIOWrapper 类型的对象不是 JSON 可序列化的
  • 非常感谢!
猜你喜欢
  • 1970-01-01
  • 2017-03-25
  • 1970-01-01
  • 2020-10-15
  • 1970-01-01
  • 2020-12-15
  • 2021-08-10
  • 2014-10-15
  • 1970-01-01
相关资源
最近更新 更多