简体   繁体   English

如何用换行符替换字符串中所有“ \\ n”实例

[英]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" +')
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)

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在字符串中用“ /”替换“ \\ /”的所有实例? - How to replace all instances of “\/” with “/” in a string? 如何使用 Python 将 csv 文件中的 /N 的所有实例替换为 NaN - How to replace all instances of /N with NaN in a csv file using Python 从字符串中删除“\\n”,但保留实际的换行符? - Remove "\n" from a string, but leave the actual linebreaks? .replace() 字典中字符串的所有实例的最佳方法 - Best way to .replace() all instances of a String in a dictionary 如何在没有Python中的replace()function的情况下替换字符串中句子中所有实例中的字母(例如ABC)? - How to replace letters (ex. ABC) in all instances in a sentence in a string without the replace() function in Python? 在python中替换字符串中除N、n和空格之外的所有元素 - Replace all elements in a string except N, n and spaces in python 如何替换字符串中第一个字母的所有实例,而不替换第一个字母本身 - How do I replace all instances of the first letter in a string, but not the first letter itself 如何用 pandas dataframe 中的字符串中间的数字零 (0) 替换破折号 (-) 的所有实例? - How do i replace all instances of a dash (-) with the number zero (0) in the middle of a string in pandas dataframe? 如何替换字符串中的 `\n` 而不是 Python 中的 `\n\n`? - How do I replace `\n` in a string but not `\n\n` in Python? 替换在另一个数据框中找到的一个数据框中的字符串的所有实例 - Replace all instances of a string on one dataframe that are found in another dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM