【问题标题】:Remove line of text from a Json file从 Json 文件中删除文本行
【发布时间】:2021-05-11 04:14:46
【问题描述】:

我正在编写一个包含物品、库存、货币等的经济机器人,但是现在当我在库存中出售我的物品时,它仍然存在于嵌入中,但我希望当我出售我的物品和编号时库存为 0 即不再显示在库存嵌入中,可以说是从 Json 文件中删除

我的销售命令代码:

@client.command()
async def sell(ctx,item,amount = 1):
    await open_account(ctx.author)

    res = await sell_this(ctx.author,item,amount)

    em1 = discord.Embed(title=f"{ctx.author.name}",
                        description="Das item konnte nicht in deinem Inventar gefunden werden",
                        color=0xe67e22)
    em1.set_thumbnail(url=ctx.author.avatar_url)
    em2 = discord.Embed(title=f"{ctx.author.name}",
                        description=f"Du hast keine {amount} {item} in deinem inventar",
                        color=0xe67e22)
    em2.set_thumbnail(url=ctx.author.avatar_url)
    em3 = discord.Embed(title=f"{ctx.author.name}",
                        description=f"Du hast das Item: **{item}** nicht in deinem Inventar",
                        color=0xe67e22)
    em3.set_thumbnail(url=ctx.author.avatar_url)
    em4 = discord.Embed(title=f"{ctx.author.name}",
                        description=f"Du hast {amount} {item} gekauft",
                        color=0xe67e22)
    em4.set_thumbnail(url=ctx.author.avatar_url)

    if not res[0]:
        if res[1]==1:
            await ctx.send(embed=em1)
            return
        if res[1]==2:
            await ctx.send(embed=em2)
            return
        if res[1]==3:
            await ctx.send(embed=em3)
            return

    await ctx.send(embed=em4)

async def sell_this(user,item_name,amount,price = None):
    item_name = item_name.lower()
    name_ = None
    for item in mainshop:
        name = item["name"].lower()
        if name == item_name:
            name_ = name
            if price==None:
                price = 0.9* item["price"]
            break

    if name_ == None:
        return [False,1]

    cost = price*amount

    users = await get_bank_data()

    bal = await update_bank(user)


    try:
        index = 0
        t = None
        for thing in users[str(user.id)]["bag"]:
            n = thing["item"]
            if n == item_name:
                old_amt = thing["amount"]
                new_amt = old_amt - amount
                if new_amt < 0:
                    return [False,2]
                users[str(user.id)]["bag"][index]["amount"] = new_amt
                t = 1
                break
            index+=1
        if t == None:
            return [False,3]
    except:
        return [False,3]



    with open("Bank.json","w") as f:
        json.dump(users,f)

    await update_bank(user,cost,"wallet")

    return [True,"Worked"]

希望有人能帮助我

【问题讨论】:

    标签: json discord discord.py


    【解决方案1】:

    使用del从json数据中删除有问题的项目:

        try:
            index = 0
            t = None
            for thing in users[str(user.id)]["bag"]:
                n = thing["item"]
                if n == item_name:
                    old_amt = thing["amount"]
                    new_amt = old_amt - amount
                    if new_amt < 0:
                        return [False,2]
                    elif new_amt == 0: # Check if amount is 0
                        del users[str(user.id)]["bag"][index] # Delete item from bag
                    else:
                        users[str(user.id)]["bag"][index]["amount"] = new_amt
                    t = 1
                    break
                index+=1
            if t == None:
                return [False,3]
        except:
            return [False,3]
    

    这会从“袋子”中完全移除物品和数量。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-22
      • 2011-01-05
      • 2013-05-13
      • 2013-09-01
      • 2012-03-28
      • 2010-11-17
      • 1970-01-01
      相关资源
      最近更新 更多