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

2024-06-01 02:12:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我用BeautifulSoup来提取一个网站,得到了一个需要的文本。问题是文本中有“\n”字符,需要删除。在

输出文本示例:

\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. But I was shocked.\nShocked to find a film starring Costner that appealed to the soul of\nthe audience. Shocked that Ashton Kutcher could act in such a serious\nrole. 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. Kutcher seems to have tweaked the serious\nacting he played with in "Butterfly Effect". 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. 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

我尝试了以下方法来删除新行。在

方法1-正则表达式

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

方法2-rstrip

^{pr2}$

这两种方法都不适合我。在

当我使用分割时

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

输出如下

['\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. But I was shocked.\nShocked to find a film starring Costner that appealed to the soul of\nthe audience. Shocked that Ashton Kutcher could act in such a serious\nrole. 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. Kutcher seems to have tweaked the serious\nacting he played with in "Butterfly Effect". 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. 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']

我该怎么做才能从文本中删除新行。在

谢谢。在


Tags: andofthetoin文本anthat
3条回答

你认为它是'\n'字符而不是{}两个字符序列吗?如果是'\n',x.rstrip()应该可以,否则请尝试x.replace('\\n','')

如果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

然后s.strip()将删除尾随空格和前导空格,包括换行符:

^{pr2}$

若要删除所有其他项,\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", "")去掉换行符。在

相关问题 更多 >