Python str.strip()错误,我的strip没有删除str中的所有引号
错误输出: 这是我代码的当前输出Python str.strip()错误,我的strip没有删除str中的所有引号,python,mysql,python-3.x,discord.py,strip,Python,Mysql,Python 3.x,Discord.py,Strip,错误输出: 这是我代码的当前输出 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_
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="stripped = str(monresult).replace("'", "")
@client.command
异步def statsctx:
成员=ctx.author
尝试:
使用connection.cursor作为游标:
读一条记录
sql=从用户ID=%s的玩家中选择xp\u点数
values=member.id
cursor.executesql,值
结果=cursor.fetchone
例外情况除外,如e:
发生printfAn错误>{e}
尝试:
使用connection.cursor作为游标:
monsql=从用户ID=%s的玩家中选择金钱
value=member.id
cursor.executemonsql,值
monresult=strcursor.fetchone
stripped=strmonresult.strip{'}
打印剥离
例外情况除外,如e:
发生printfAn错误>{e}
e=discord.Embedtitle=Stats命令,color=member.color
e、 add_fieldname=经验点,value=结果
e、 添加\u字段名=
strip方法会导致问题,因为您不知道有时会剥离什么,所以在较新版本的python中会有removeprefix和removesuffix。使用replace会更容易,你甚至可以指定应该被替换的出现次数。使用replace而不是strip。使用strmonresult.replace',而不是strip真棒,真的很感谢你,伙计。我已经给出了一个答案,你可以验证