Home > Mobile >  Unable to remove new line characters from string
Unable to remove new line characters from string

Time:08-03

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: [email protected]\n\nLuca\nXXX\n\nvideo sponsored by manual.

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

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: [email protected]\n\nLuca\nXXX\n\nvideo sponsored by manual.

CodePudding user response:

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: [email protected]\\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', ' ').

  • Related