python - 尝试在带有发誓的嵌入中插入,不适用于最后一个列表中的字符串
问题描述
我正在尝试制作一个城市词典命令,我正在用“[NSFW]”这个词替换脏话,尽管它仅在我将脏话放在列表的最后一部分时才有效。
我的代码:
nsfwdefine = ["swear1", "swear2","swear3"]
// there are headers I just didn't add them because it has my apikey.
async with ClientSession() as session:
async with session.get(url, headers=headers, params=querystring) as response:
r = await response.json()
embed = discord.Embed(title=f"All result for {term}", description=None,color=0xff0000)
embed.set_author(name=ctx.author.display_name, url="https://ass.com/", icon_url=ctx.author.avatar_url)
definition = r['list'][0]['definition']
embed.add_field(name=term, value=definition, inline=False)
txt = str(definition)
for y in nsfwdefine:
x = re.sub(y, '[NSFW]', txt)
print(x)
embed2=discord.Embed(title=f"All results for {term}", description=x)
embed2.add_field(name="Info",value=":warning: If your message contains the word [NSFW] then you need to rerun the defintion in a NSFW Channel.")
(await ctx.send(embed = embed2))
我不完全确定发生了什么,有人知道解决方案吗?
解决方案
我不确定您为什么要使用,尽管您可以使用 string方法re.sub()
来实现这一点。replace()
swears = ["swear1", "swear2", "swear3"]
sentence = "hello swear1 this is swear2 a sample swear3 sentence!"
for word in swears:
sentence = sentence.replace(word, "[NSFW]")
print(sentence)
输出:
hello [NSFW] this is [NSFW] a sample [NSFW] sentance!
推荐阅读
- java - 封闭的应用程序如何接收定期广播?
- python - 如何为我的 selenium 脚本选择此文本框?
- sql-server - SQL Server 代理运行 SSIS 无法将文件移动到不同的服务器(无域)
- c++ - 有人可以指出我错在哪里吗?
- node.js - 导出 https 以表达用于新 WebSocket 的路由
- python - os.path.join 返回“C:/...”
- ios - 收到推送通知且应用程序在前台时如何防止弹出警报
- authentication - 回调团队消息扩展自适应卡
- sql - 按印度和国际的百分比查找工作类别的 ctc 比率
- laravel - 在vue js中将ag网格表转换为PDF文件