0

I've perused the available threads and nothing worked. Tried this approach with the double-slashes, it didn't work. Also tried the raw-string approach as r\n and that didn't work either.

I need to replace all newlines (let's start with \n) to <br/>. See the following screenshots showing how the result doesn't change from the original string, which is obj.get(key):

.replace('\\n', '<br/>')
.replace(r'\n', '<br/>')

enter image description here enter image description here

gene b.
  • 10,512
  • 21
  • 115
  • 227
  • 3
    If they're actually newline characters, don't use escapes or raw strings to make it a literal backslash followed by `'n'`, just do `.replace('\n', '
    ')` to replace actual newlines.
    – ShadowRanger Apr 03 '22 at 16:34
  • Where did you call the `getWithHTMLBreaksForNewlinesOrTrimToNone()` function? – Red Apr 03 '22 at 16:34
  • Thanks, `ShadowRanger`'s plain `\n` worked. However, when I wanted to include the multiple options `\r\n` it didn't work. I got into this situation by trying to include both `\r` and `\n`. – gene b. Apr 03 '22 at 16:40
  • 1
    `'
    '.join(text.splitlines())`.
    – ekhumoro Apr 03 '22 at 16:45
  • 1
    This isn't a proper duplicate - the other question wasn't really about how to do this, but about a problem with using `.replace`. This question is about a **different** problem with using `.replace`, which should probably be considered a typo. – Karl Knechtel Aug 06 '22 at 01:37

1 Answers1

0

Try to convert the text to stream in order to call readlines, and replace the last character (carriage return by )

Like this

>>> msg = "Bob Smith\nJane Doe\nJane,\nPlease order more widgets\nThanks,\nBob\n"
>>> msg
'Bob Smith\nJane Doe\nJane,\nPlease order more widgets\nThanks,\nBob\n'
>>>
>>> import io
>>> buf = io.StringIO(msg)
>>> buf.readline()[-1]='<br>'

You need to iterate over buff

Tlaloc-ES
  • 4,825
  • 7
  • 38
  • 84