在控制台和html和文本中输出python

时间:2014-06-14 17:06:10

标签: python html text

我使用python编写了以下代码,我希望输出在console,text和html中 html输出中的问题,在下面的输出中,有两列,第一列是键,另一列是控制台中的值,文本中它们之间有空格,但在html中没有任何空格

print f
x = "John"
g = "Peter"


if re.search(x,f) :
    print temp.format("It contains  John",2)
if re.search(g,f):
    print temp.format("It contains  Peter",5)
if re.search(x,f) and re.search(g,f):
    print "It contains both persons"

sys.stdout = save_stdout    
print capture.getvalue()
template = """
<html>
    <head></head>
    <body>
        <p>%(text)s</p>
    </body>
</html>
"""


count = capture.getvalue().count('\n')-1
html_message = template %{"text":capture.getvalue().replace('\n', "<br>\n", count)}


f2.write(html_message+"\n")
f2.close()

f1.write(capture.getvalue())
f1.close()

=============================================== ===============================

input : "Peter and John went to London"
output in console:
Peter and John went to London
It contains  John       2
It contains  Peter      5
It contains both persons 

but in html:
Peter and John went to London
It contains John 2
It contains Peter 5
It contains both persons 

在html的输出中,没有像控制台和文本那样的空间。 任何人都能帮我这个吗?

1 个答案:

答案 0 :(得分:0)

最简单的解决方案 - 使用<pre>%(text)s</pre>

<pre>显示所有空格,标签和&#34; \ n&#34; (你不需要<br>


对于一个参数,您可以使用

import sys

if len(sys.argv) > 1: 
   if sys.argv[1] == '--h':
      print "create HTML"
   elif sys.argv[1] == '--t':
      print "create TEXT"

对于更复杂的参数(安装和),请使用模块getoptargparse


enter image description here

enter image description here