如何在python的html textarea中存储多行文本?

时间:2013-12-16 21:57:47

标签: python html

我有一个sql数据库,其中每一行都包含文本段落。我想在它自己的html textarea中显示每行文本。我正在使用python生成html页面。我尝试过以下但是textarea的主体总是空的。有谁知道我能做些什么来解决这个问题?

# didn't work
# dict = {'example':results[i]["Teaser"].replace("\"", "\\\"").replace("<", "&lt;").replace(">", "&gt;"), 'arg':results[i]["GImageID"]}
# s = "<br><textarea class=\"\" rows=\"2\" cols=\"20\" id=\"extra{arg}\">maybe1 - {example}</textarea>".format(**dict)

# gives an error on the d even though a tutorial said I should do it this way
# s = "<br><textarea class=\"\" rows=\"2\" cols=\"20\" id=\"extra{arg:d}\">maybe2 - {example:d}</textarea>".format(**dict)

#extra += s

#------------------------------------------------------
teaser = results[i]["Teaser"].replace("\"", "\\\"").replace("<", "&lt;").replace(">", "&gt;")


# didn't work - empty
# extra += "<br><textarea class=\"\" rows=\"2\" cols=\"20\" id=\"extra{arg}\">maybe3 - {example}</textarea>".format(example=teaser, arg=results[i]["GImageID"])

# didn't work - empty
# extra += "<br><textarea class=\"\" rows=\"2\" cols=\"20\" id=\"extra{arg}\">maybe3.5 - {example}</textarea>".format(example=results[i]["GImageTeaser"].replace("\"", "\\\"").replace("<", "&lt;").replace(">", "&gt;"), arg=results[i]["GImageID"])

# didn't work - empty
# t = Template("<br><textarea class=\"\" rows=\"2\" cols=\"20\" id=\"extra$arg\">maybe4 - $example</textarea>")
# s = t.substitute({ 'example': results[i]["Teaser"].replace("\"", "\\\"").replace("<", "&lt;").replace(">", "&gt;"), 'arg': results[i]["GImageID"]})
# extra += s

# didn't work - empty
# html = Template("<br><textarea class=\"\" rows=\"2\" cols=\"20\" id=\"extra$arg\">maybe5 - $example</textarea>")
# result = html.safe_substitute(example=teaser,arg='Vishnu')

# result = html.safe_substitute(example=results[i]["Teaser"].replace("\"", "\\\"").replace("<", "&lt;").replace(">", "&gt;"),arg='Vishnu')
# extra += result

# didn't work - empty
# extra += """<br><textarea class=\"\" rows=\"2\" cols=\"20\" id=\"extra%d\">maybe6 - %s</textarea>""" % (results[i]["GImageID"], results[i]["Teaser"].replace("\"", "\\\"").replace("<", "&lt;").replace(">", "&gt;"))

上面的文字是for循环

for i in range(len(results)):

我输出了文本结果[i] [“Teaser”]来验证它是不是空的。 GImageID打印得很好,但包含段落的Teaser文本始终为空。

如果您需要任何其他信息,请告诉我们。我被困了几个小时,我找不到任何有效的解决方案。谢谢!

1 个答案:

答案 0 :(得分:0)

import cgi

text = results[i]["Teaser"] # the text that you want to put into textarea
html_safe_text = cgi.escape(text) # < > & -> &lt; &gt; &amp;
textarea_html = u"<textarea>{}</textarea>".format(html_safe_text)

您可以在浏览器中打开它进行调试:

import tempfile
import webbrowser
import time

def open_in_browser(html):
    """like lxml.html.open_in_browser() but `html` is a bytestring."""
    with tempfile.NamedTemporaryFile("wb", 0, suffix='.html') as file:
        file.write(html)
        webbrowser.open(file.name)
        time.sleep(60) # give the browser a minute to open before
                       # deleting the file

open_in_browser((u'''<!doctype html>
    <meta charset="utf-8">
    <title>Fill textarea</title>
''' + textarea_html).encode('utf-8'))

以下是您可以尝试的独立fill-textarea.py script