I
I
Ilya2020-08-05 13:48:57
Python
Ilya, 2020-08-05 13:48:57

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:
5f2a8d2bd78bb018513515.png

I want to remove \n\r leaving just \n, I replace it in this way, everything works fine in Notepad++:
5f2a8dd253e74572815879.png

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', '')


Help, plz

PS Maybe it has something to do with Notepad itself? Because when copying these lines to another location, it makes some changes:
5f2a90af56f5b661924640.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
soremix, 2020-08-05
@SoreMix

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 question

Ask a Question

731 491 924 answers to any question