Answer the question
In order to leave comments, you need to log in
How to remove \n\r from a document?
Using Python, I throw data into a text document, after formatting it.
It turns out the following text:
I want to remove \n\r leaving just \n, I replace it in this way, everything works fine in Notepad++:
But when I try to correct the text in Python before writing it to the document, nothing happens. Tried these two options:
articleContent = articleContent.replace('\n\r', '\n')
#или это
articleContent = articleContent.replace('\n\r', '')
Answer the question
In order to leave comments, you need to log in
You have \r\n in your notepad.
Different OSes have different options for line wrapping. Python, when writing \n to a file, will convert it to a line break for the current OS. On Windows it's \r\n, on Linux it's \n, on Mac it's \r.
When reading, most likely, in the same way, all this diversity (\r\n, \n, \r) is converted to one \n in order to support cross-platform. So you don't have any \r in your text , only \n
You can check by doing
articleContent.count('\r')
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question