str.strip()错误,我的strip没有删除str中的所有引号

2024-09-30 20:22:35 发布

您现在位置:Python中文网/ 问答频道 /正文

错误输出:

这是我代码的当前输出

money': 2200

预期产出:

money: 2200

当前代码:

@client.command()
async def stats(ctx):
    member = ctx.author
    # try:
    #     with connection.cursor() as cursor:
    #         # Read a single record
    #         sql = "SELECT xp_points FROM players WHERE userid = %s"
    #         values = member.id
    #         cursor.execute(sql, values)
    #         result = cursor.fetchone()
    # except Exception as e:
    #     print(f"An error Occurred>  {e}")
    try:
        with connection.cursor() as cursor:
            monsql = "SELECT money FROM players WHERE userid = %s"
            value = member.id
            cursor.execute(monsql, value)
            monresult = str(cursor.fetchone())
            stripped = str(monresult).strip("{'}")
            print(stripped)
    except Exception as e:
        print(f"An error Occurred>  {e}")
    # e = discord.Embed(title="Stats Command", color=member.color)
    # e.add_field(name="Experience Points", value=result)
    # e.add_field(name="💰Coins Gained", value=monresult)
    # await ctx.send(embed=e, content=None)

为什么我的代码只剥离/删除字符串的一个引号?它不应该去掉所有找到的引号吗?希望有人能帮我


Tags: 代码fromsqlvalueaswithconnectionselect
2条回答
stripped = str(monresult).replace("'", "")

strip方法会导致问题,因为有时您不知道会剥离什么,所以在较新版本的python中会有removeprefixremovesuffix。使用replace()会更容易,您甚至可以指定应该替换的出现次数

使用replace()代替strip()

相关问题 更多 >