user6536526
user6536526

Reputation:

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. The problem is it has "\n" character in the text which I need to remove.

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. 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

I tried the below methods to remove the new line.

method 1 - regex

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

method 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. 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']

what should I do to remove the new lines from the text.

Thank you.

Upvotes: 1

Views: 7139

Answers (3)

olliejday
olliejday

Reputation: 196

If s is, a string such as:

\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 whitespace, which includes newlines:

 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

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!

Upvotes: 3

mugiseyebrows
mugiseyebrows

Reputation: 4698

Are you sure it's '\n' character and not '\\n' two character sequence? If it's '\n', x.rstrip() should work. Otherwise, try x.replace('\\n','')

Upvotes: 5

Ali Camilletti
Ali Camilletti

Reputation: 135

You should be able to use x=x.replace("\n", "") to take out the newline.

Upvotes: 1

Related Questions