繁体   English   中英

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

[英].strip("\n") not removing "\n" from string

 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/376​​515-jeff-flake-there-will-be-a-republican-challenger-to-trump-in-2020 \\r\\n” 和self.text仍然相同(不删除\\r\\n )。

当这段代码放在 Python 的 Shell 上时,它会按需要工作。 有任何想法吗?

编辑:当print语句改为print("REPR:"+repr(text)+"\\nTEXT:"+text+";SELFTEISXT:"+self.text)

类似字符串的输出是:

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

尝试这个:

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

尝试使用原始字符串。 我在replace(r"\\r\\n", "")添加了"r"

前任:

text =  """http://thehill.com/homenews/senate/376515-jeff-flake-there-will-be-a-republican-challenger-to-trump-in-2020\r\n"""
print(text)
print(text.replace(r"\r\n", "").strip())

输出:

http://thehill.com/homenews/senate/376515-jeff-flake-there-will-be-a-republican-challenger-to-trump-in-2020\r\n
http://thehill.com/homenews/senate/376515-jeff-flake-there-will-be-a-republican-challenger-to-trump-in-2020

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM