str.strip错误,我的测试条未从str中删除所有引号

问题描述

错误输出:

这是我的代码的当前输出

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)

为什么我的代码仅去除/删除字符串的一个引号?它不应该删除找到的所有引号吗?希望有人可以帮助我。

解决方法

使用replace()代替strip()。

,
stripped = str(monresult).replace("'","")

strip方法会引起问题,因为您有时并不真正知道将要剥离的内容,因此在较新版本的python中将有removeprefixremovesuffix。使用replace()会更容易,甚至可以指定应该替换的出现次数。

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...