如何更换“\ n”的所有实例与换行符的字符串

问题描述 投票:0回答:2

我使用从网站数据刮掉,使我的机器人在显示单独的行。有一个特定的字符串,其信息具有“\ n”个之间,我想转换成那些实际换行符

我曾试图与“+‘\ n’+”代替“\ n”,但输出仍然是相同的

这是我有问题的例子,而不是实际的机器人代码

import discord
from discord.ext.commands import Bot
from discord.ext import commands
import asyncio

Client = discord.Client()
client = commands.Bot(command_prefix = "!")

cardName = "Card Name"
rawCardText = "This is an ability\nThis is another ability"
cardText = rawCardText.replace('\n', '+ "\n" +')

fullText = cardName + "\n" + cardText
await client.send_message (message.channel, fullText)

我希望是这样的:

持卡人姓名

这是一种能力

这是另一种能力

相反,我得到的是:

持卡人姓名

这是一种能力\ n这是另一种能力

python discord.py
2个回答
1
投票

"\n"raw string

cardText = rawCardText.replace(r'\n', '+ "\n" +')

1
投票
cardName = "Card Name"
rawCardText = "This is an ability\nThis is another ability"
cardText = rawCardText.split('\n')
fullText = fullText = (cardName + '\n' + '\n'.join(cardText))
print (fullText)

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.