.strip(“\ n”)不从字符串中删除“\ n”

问题描述 投票:-4回答:2
 def __init__(self, text):
     self.text = text.strip("\r").strip("\n").strip("\r").strip(" ")
     print("TEXT:"+text+";SELFTEXT:"+self.text)

text被称为“http://thehill.com/homenews/senate/376515-jeff-flake-there-will-be-a-republican-challenger-to-trump-in-2020 \ r \ n”,而self.text仍然是相同的(不会删除\r\n)。

当这个代码放在Shell for Python上时,它可以根据需要运行。有任何想法吗?

编辑:当print语句更改为print时(“REPR:”+ repr(text)+“\ TEXT:”+ text“; SELF TEXT:”+ self.text)

类似字符串的输出是:

REPR:'http://thehill.com/homenews/senate/376548-lindsey-graham-war-with-north-korea-would-be-worth-it-in-the-long-run\r\n'文字:http://thehill.com/homenews/senate/376548-lindsey-graham-war-with-north-korea-would-be-worth-it-in-the-long-run \ r \ n; SELFTEISXT:http://thehill.com/homenews/senate/376548-lindsey-graham-war-with-north-korea-would-be-worth-it-in-the-long-run \ r \ n

python string strip
2个回答
1
投票

试试这个:

text.replace("\r\n","")

0
投票

尝试使用原始字符串。我在qazxsw poi中添加了qazxsw poi

EX:

"r"

输出:

replace(r"\r\n", "")
© www.soinside.com 2019 - 2024. All rights reserved.