简体   繁体   English

如何在python中的字符串中删除换行符

[英]How can I remove a newline character in a string in python

I use BeautifulSoup to extract a website and I got a the text need. 我使用BeautifulSoup提取了一个网站,然后得到了文本需求。 The problem is it has "\\n" character in the text which I need to remove. 问题是它在我需要删除的文本中具有“ \\ n”字符。

sample output text: 示例输出文本:

\\nI went to an advance screening of this movie thinking I was about to\\nembark on 120 minutes of cheezy lines, mindless plot, and the kind of\\nnauseous acting that made "The Postman" one of the most malignant\\ndisplays of cinematic blundering of our time. \\ n我以这部电影的预告片为准,想在\\分钟120分钟的通俗易懂的线条,无心的情节以及那种使\\“邮递员”成为最恶毒的电影错误\\ n的恶作剧行为\\ n下车我们这个时代。 But I was shocked.\\nShocked to find a film starring Costner that appealed to the soul of\\nthe audience. 但是我很震惊。\\ n震惊地发现了由科斯特纳(Costner)主演的电影,它吸引了观众的灵魂。 Shocked that Ashton Kutcher could act in such a serious\\nrole. 震惊的是阿什顿·库彻(Ashton Kutcher)可以扮演如此严肃的角色。 Shocked that a film starring both actually engaged and captured\\nmy own emotions. 震惊的是这部由主演的电影实际上参与并捕捉了自己的情感。 Not since 'Robin Hood' have I seen this Costner: full\\nof depth and complex emotion. 自从《罗宾汉》问世以来,我还没有看到过这个Costner:充满\\ n深度和复杂的情感。 Kutcher seems to have tweaked the serious\\nacting he played with in "Butterfly Effect". 库彻(Kutcher)似乎已经调整了他在《蝴蝶效应》中扮演的严肃角色。 These two actors came into\\nthis film with a serious, focused attitude that shone through in what I\\nthought was one of the best films I've seen this year. 这两位演员以严肃认真,专注的态度进入了这部电影,在我认为这是我今年看过的最好的电影之一中透出了光芒。 No, its not an\\nOscar worthy movie. 不,这不是一部\\ n奥斯卡值得的电影。 It's not an epic, or a profound social commentary\\nfilm. 这不是一部史诗,也不是一部深刻的社会评论\\ n电影。 Rather, its a story about a simple topic, illuminated in a way\\nthat brings that audience to a higher level of empathy than thought\\npossible. 相反,它是一个有关简单主题的故事,以某种方式阐明\\ n,使该受众具有比想像更高的同理心。 That's what I think good film-making is and I for one am\\nthroughly impressed by this work. 我认为这就是出色的电影摄制,这部作品给我留下了深刻的印象。 Bravo!\\n 好极了!\\ n

I tried the below methods to remove the new line. 我尝试了以下方法删除新行。

method 1 - regex 方法1-正则表达式

x = review_text.get_text()
y = re.sub(r'(\n)','',x)

method 2 - rstrip 方法2-rstrip

x = review_text.get_text()
x.rstrip()

Neither of this methods are working for me. 这两种方法都不适合我。

When I use split 当我使用拆分

x = review_text.get_text()
print(x.split("\n"),"\n\n")

The output is as follows 输出如下

['\\nI went to an advance screening of this movie thinking I was about to\\nembark on 120 minutes of cheezy lines, mindless plot, and the kind of\\nnauseous acting that made "The Postman" one of the most malignant\\ndisplays of cinematic blundering of our time. ['\\ n我想提前看完这部电影,以为我打算\\在120分钟的通俗易懂的线条,无心的情节以及那种使\\“邮递员”成为最恶毒的电影之一的\\ n恶作剧\\ n我们时代的电影失误。 But I was shocked.\\nShocked to find a film starring Costner that appealed to the soul of\\nthe audience. 但是我很震惊。\\ n震惊地发现了由科斯特纳(Costner)主演的电影,它吸引了观众的灵魂。 Shocked that Ashton Kutcher could act in such a serious\\nrole. 震惊的是阿什顿·库彻(Ashton Kutcher)可以扮演如此严肃的角色。 Shocked that a film starring both actually engaged and captured\\nmy own emotions. 震惊的是这部由主演的电影实际上参与并捕捉了自己的情感。 Not since \\'Robin Hood\\' have I seen this Costner: full\\nof depth and complex emotion. 自从《罗宾汉》问世以来,我还没有看过这本科斯特纳:充满\\ n深度和复杂的情感。 Kutcher seems to have tweaked the serious\\nacting he played with in "Butterfly Effect". 库彻(Kutcher)似乎已经调整了他在《蝴蝶效应》中扮演的严肃角色。 These two actors came into\\nthis film with a serious, focused attitude that shone through in what I\\nthought was one of the best films I\\'ve seen this year. 这两位演员以严肃认真,专注的态度进入了这部电影,这在我认为是我今年看过的最好的电影之一中得到了体现。 No, its not an\\nOscar worthy movie. 不,这不是一部\\ n奥斯卡值得的电影。 It\\'s not an epic, or a profound social commentary\\nfilm. 这不是一部史诗级电影,也不是一部深刻的社会评论电影。 Rather, its a story about a simple topic, illuminated in a way\\nthat brings that audience to a higher level of empathy than thought\\npossible. 相反,它是一个有关简单主题的故事,以某种方式阐明\\ n,使该受众具有比想像更高的同理心。 That\\'s what I think good film-making is and I for one am\\nthroughly impressed by this work. 这就是我认为出色的电影制作所能为我留下的深刻印象。 Bravo!\\n'] 好样的!\\ n']

what should I do to remove the new lines from the text. 我应该怎么做才能从文本中删除新行。

Thank you. 谢谢。

Are you shure it's '\\n' character and not '\\\\n' two character sequence? 您确定它是'\\n'字符而不是'\\\\n'两个字符序列吗? If it's '\\n', x.rstrip() should work, otherwise try x.replace('\\\\n','') 如果为'\\ n',则x.rstrip()应该可以工作,否则请尝试x.replace('\\\\n','')

If s is, a string such as: 如果s是,则字符串如:

\nNo, its not an\nOscar worthy movie. It's not an epic, or a profound social commentary\nfilm. Rather, its a story about a simple topic, illuminated in a way\nthat brings that audience to a higher level of empathy than thought\npossible. That's what I think good film-making is and I for one am\nthroughly impressed by this work. Bravo!\n

then s.strip() will remove trailing and leading spaces, including newlines: 然后s.strip()将删除尾随和前导空格,包括换行符:

 No, its not an\nOscar worthy movie. It's not an epic, or a profound social commentary\nfilm. Rather, its a story about a simple topic, illuminated in a way\nthat brings that audience to a higher level of empathy than thought\npossible. That's what I think good film-making is and I for one am\nthroughly impressed by this work. Bravo!

To remove all the other \\n, replace them with " " for a space or "" to remove completely 要删除所有其他\\ n,请将其替换为“”,或将其完全删除

s.replace("\n", "").strip()

No, its not an Oscar worthy movie. It's not an epic, or a profound social commentary film. Rather, its a story about a simple topic, illuminated in a way that brings that audience to a higher level of empathy than thought possible. That's what I think good film-making is and I for one am throughly impressed by this work. Bravo!

您应该可以使用x=x.replace("\\n", "")取出换行符。

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

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