Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Unable to remove new line characters from string

I am trying to parse the links from the strings in Python. I got the following string:

Hair loss can be all-consuming. In todays video, I start by sharing some holistic practices I personally followed to combat hair loss, such as; changing my diet, reducing stress levels and changing hairstyles. I also share my long-term plan for the near future to com hair loss, such as; changing my diet, reducing stress levels and changing hairstyles. I also share my long-term plan for the near future to combat hair thinning and loss.\n\nFor 50% off your first order – https://link.manual.co/LUCA5010\n\nShop my favourite products (affiliate) – https://www.amazon.co.uk/shop/https://www.amazon.co.uk/shop/lucasantangelo\nsay hi over on instagram @lucazadeee / https://www.instagram.com/lucazadeee/\nFor business enquiries only: Lucasantangelo@hotmail.com\n\nLuca\nXXX\n\nvideo sponsored by manual.

First of all I removed the new line characters from the string using the code

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

text = " ".join(text.splitlines())

But I checked the results that it is not removing \n from the links and I am getting the following text after the above code is executed:

Hair loss can be all-consuming. In todays video, I start by sharing some holistic practices I personally followed to combat hair loss, such as; changing my diet, reducing stress levels and changing hairstyles. I also share my long-term plan for the near future to combat hair thinning and loss.\n\nFor 50% off your first order – https://link.manual.co/LUCA5010\n\nShop my favourite products (affiliate) – https://www.amazon.co.uk/shop/https://www.amazon.co.uk/shop/lucasantangelo\nsay hi over on instagram @lucazadeee / https://www.instagram.com/lucazadeee/\nFor business enquiries only: Lucasantangelo@hotmail.com\n\nLuca\nXXX\n\nvideo sponsored by manual.

>Solution :

As your output from print(repr(text))

'I also share my long-term plan for the near future to combat hair thinning and loss.\\n\\nFor 50% off your first order - link.manual.co/LUCA5010\\n\\nShop my favourite products (affiliate) - amazon.co.uk/shop/https://www.amazon.co.uk/shop/lucasantangelo\\nsay hi over on instagram @lucazadeee / instagram.com/lucazadeee\\nFor business enquiries only: Lucasantangelo@hotmail.com\\n\\nLuca\\nXXX\\n\\nvideo sponsored by manual.'

suggests, \\n is escaped. So try: text.replace('\\n', ' ') instead, or using raw string, text.replace(r'\n', ' ').

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading