str.strip() 错误,我的 strip 没有删除我的 str 中的所有引号
str.strip() error, my strip does not remove all the quotation marks from my 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)
为什么我的代码只有stripping/removing字符串的一个引号?它不应该去掉它找到的所有引号吗?希望有人能帮我解决这个问题。
stripped = str(monresult).replace("'", "")
strip 方法会导致问题,因为有时您真的不知道什么会被剥离,因此在 python 的较新版本中会有 removeprefix
和 removesuffix
。使用 replace()
会更容易,您甚至可以指定应替换的出现次数。
使用 replace() 而不是 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_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)
为什么我的代码只有stripping/removing字符串的一个引号?它不应该去掉它找到的所有引号吗?希望有人能帮我解决这个问题。
stripped = str(monresult).replace("'", "")
strip 方法会导致问题,因为有时您真的不知道什么会被剥离,因此在 python 的较新版本中会有 removeprefix
和 removesuffix
。使用 replace()
会更容易,您甚至可以指定应替换的出现次数。
使用 replace() 而不是 strip()。