views:

743

answers:

5

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.

I spent hours searching for the solution to no avail. Can anyone help?

A: 

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.

The MYYN
Yes, in fact newlines ARE working! It seems that I am saying<br><pre>print("Content-type:text/html\n\n", HTML.encode("utf-8"))</pre><br>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:<br><pre>UnicodeEncodeError: 'ascii' codec can't encode character '\u8e47' in position 14525: ordinal not in range(128)<pre><br>So what is happening?Can anyone tell me how to format text on this site?
Gnarlodious
+2  A: 

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

S.Mark
I would suggest to first replace \r\n with \n and then \n with <br/> so you don't end up with some \r in your string.
Brian R. Bondy
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
how about `=s.replace("","\n")` Gnarlodious?
S.Mark
@Gnarlodious: The question "backslash escaped newlines". Does that mean "\\n"? A backslash character (\) and an n? If so, `replace( "\\n", "<br />" )` would work.
S.Lott
@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 `<BR>`s in there, just not all the HTML on one line. I think the desired code is `s = s.replace(r'\n','\n')`.
Paul McGuire
A: 

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
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
A: 

It turns out that Chinese characters (晉) in the string is causing Unicode to do something unknown. But I'm getting closer!

Gnarlodious
A: 

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