7

I have upgraded to Python 3 and can't figure out how to convert backslash escaped newlines to HTML.

The browser renders the backslashes literally, so "\n" has no effect on the HTML source. As a result, my source page is all in one long line and impossible to diagnose.

agf
  • 171,228
  • 44
  • 289
  • 238
Gnarlodious
  • 304
  • 2
  • 4
  • 16

6 Answers6

7

normally I do like this s=s.replace("\n","<br />\n")

because

<br /> is needed in web page display and

\n is needed in source display.

just my 2 cents

YOU
  • 120,166
  • 34
  • 186
  • 219
  • 2
    I would suggest to first replace \r\n with \n and then \n with
    so you don't end up with some \r in your string.
    – Brian R. Bondy Nov 21 '09 at 17:49
  • This does not work since my output does not already have newlines in it. In any case, apparently Python 3 does not convert UNIX style escaped characters into the appropriate ASCII characters. – Gnarlodious Nov 22 '09 at 02:41
  • how about `=s.replace(" ","\n")` Gnarlodious? – YOU Nov 22 '09 at 04:09
  • @Gnarlodious: The question "backslash escaped newlines". Does that mean "\\n"? A backslash character (\) and an n? If so, `replace( "\\n", "
    " )` would work.
    – S.Lott Nov 22 '09 at 05:02
  • @S.Lott - G'odious is saying that his HTML source has *no* newlines in it, so the substitution string should put in a newline too. In fact, it's not clear that he/she *want* the HTML `
    `s in there, just not all the HTML on one line. I think the desired code is `s = s.replace(r'\n','\n')`.
    – PaulMcG Nov 22 '09 at 10:49
1

The solution is:

#!/usr/bin/python 
 import sys 
 def print(s): return sys.stdout.buffer.write(s.encode('utf-8'))
 print("Content-type:text/plain;charset=utf-8\n\n") 
 print('晉\n') 

See the original discussion here: http://groups.google.com/group/comp.lang.python/msg/f8bba45e55fe605c

Gnarlodious
  • 304
  • 2
  • 4
  • 16
0

Maybe I don't get it, but isn't <br /> some kind of newline for HTML?

s = "Hello HTML\n"
to_render = s.replace("\n", "<br />")

If you render something with mimetype "text/plain" \newlines should work.

miku
  • 181,842
  • 47
  • 306
  • 310
  • Yes, in fact newlines ARE working! It seems that I am saying
    print("Content-type:text/html\n\n", HTML.encode("utf-8"))

    so the conversion to UTF8 is wiping out my newlines! And actually, Python 3 is all about UTF8, so that conversion is unnecessary. However, removing the conversion gives me error:
    UnicodeEncodeError: 'ascii' codec can't encode character '\u8e47' in position 14525: ordinal not in range(128)

    So what is happening? Can anyone tell me how to format text on this site?
    – Gnarlodious Nov 22 '09 at 02:55
0

If you are using Django, this answer will be helpful.

It's about how you render the page and whether you escape HTML or no.

gevra
  • 737
  • 2
  • 14
  • 26
0

Since I have solved basic Markdown, I have resolved the new lines with a regular expression.

import re
br = re.compile(r"(\r\n|\r|\n)")  # Supports CRLF, LF, CR
content = br.sub(r"<br />\n", content)  # \n for JavaScript
Geekmoss
  • 637
  • 6
  • 11
-1

Print() should add a newline by default - unless you tell it otherwise. However there have been other changes in Python 3:

Old: print "The answer is", 2*2
New: print("The answer is", 2*2)

Old: print x,           # Trailing comma suppresses newline
New: print(x, end=" ")  # Appends a space instead of a newline

Old: print              # Prints a newline
New: print()            # You must call the function!

Old: print >>sys.stderr, "fatal error"
New: print("fatal error", file=sys.stderr)

Old: print (x, y)       # prints repr((x, y))
New: print((x, y))      # Not the same as print(x, y)!

Old = Python 2.5, New = Python 3.

More details here: http://docs.python.org/3.1/whatsnew/3.0.html

Martin
  • 39,569
  • 20
  • 99
  • 130
  • This does not work since I am assembling strings and returning the result. If I were printing directly from the script it might be OK. – Gnarlodious Nov 22 '09 at 02:43