[英]How to replace all instances of “\n” in a string with linebreaks
I am using data scraped from a website and making my bot display in separate lines. 我正在使用从网站抓取的数据,并使我的漫游器显示在单独的行中。 There is a particular string whose information has "\\n" in between and I want to convert those into actual line breaks
有一个特定的字符串,其信息之间包含“ \\ n”,我想将其转换为实际的换行符
I have tried replacing the '\\n' with '+ "\\n" +' but the output is still the same 我尝试用'+“ \\ n” +'替换'\\ n',但输出仍然相同
This is an example of the issue I have, and not the actual bot code 这是我遇到的问题的一个示例,而不是实际的机器人代码
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)
I expected something like this: 我期望这样的事情:
Card Name 卡名
This is an ability 这是一种能力
This is another ability 这是另一种能力
Instead what I get is: 相反,我得到的是:
Card Name 卡名
This is an ability\\nThis is another ability 这是一种能力\\ n这是另一种能力
将"\\n"
写为raw string
:
cardText = rawCardText.replace(r'\n', '+ "\n" +')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.