Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 3: Write newlines to HTML

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.

like image 867
Gnarlodious Avatar asked Nov 21 '09 17:11

Gnarlodious


2 Answers

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

like image 85
YOU Avatar answered Nov 12 '22 08:11

YOU


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

like image 38
Gnarlodious Avatar answered Nov 12 '22 08:11

Gnarlodious