亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

str.strip() 錯誤,我的 strip 沒有刪除我的 str 中的所有引號

str.strip() 錯誤,我的 strip 沒有刪除我的 str 中的所有引號

蕭十郎 2023-06-02 16:07:29
錯誤輸出:這是我的代碼的當前輸出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)為什么我的代碼只剝離/刪除字符串的一個引號?它不應該去掉它找到的所有引號嗎?希望有人可以幫助我。
查看完整描述

2 回答

?
月關寶盒

TA貢獻1772條經驗 獲得超5個贊

使用 replace() 而不是 strip()。



查看完整回答
反對 回復 2023-06-02
?
四季花海

TA貢獻1811條經驗 獲得超5個贊

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

strip 方法會導致問題,因為有時您真的不知道什么會被剝離,因此在較新版本的 python 中會有removeprefixand removesuffix。使用replace()會更容易,您甚至可以指定應替換的出現次數。


查看完整回答
反對 回復 2023-06-02
  • 2 回答
  • 0 關注
  • 178 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號