Python生成HTML打印列表

-2 投票
1 回答
559 浏览
提问于 2025-04-18 15:00

这是我脚本的一部分。在这段代码中,我有一个叫做 content 的列表,里面包含了一堆 ASCII 字符。我想知道怎么把这些 ASCII 字符按行和列打印出来。现在我在网址上看到的只是一个单词 "content",而不是列表里的内容。其他的都正常,所以我只需要知道怎么显示列表里的内容。

print (''' <!DOCTYPE html>
<html>
    <head>
        <title>Lab 5.1.cgi</title>
        <style type="text/css">
             body {background-color:white;font-style:none;font-weight:normal; font-family:Gill Sans, Helvetica, Arial,sans-serif;font-weight:bold;font-size:30px;color:#c00; text-shadow:0 0 2px black,0px 0px 8px white; background:url(data:image/gif;base64,%s);text-align:center} h1 {font-size:72px;font-weight:bold} h2 {font-weight:normal} div {font-family:Arial;font-size:30px;color:black; text-shadow:none;background-color:white;width:440px;margin:0 auto} .source {width:960px}
        </style>
    </head>
    <body>
        <div class="row">
            <div class="twelve columns">
                <div class="globalWrapper">
                    <div>
                        <strong>lab5.1.cgi</strong>
                        <h1 class="title">Arial Unicode MS</h1>
                        <h2 class="small">Printable Characters: 32&ndash;126, 128 &ndash; 4000</h2>
                        %s
                    </div>
                </div>
            </div>
        </div>
    </body>
</html>''' % (external_link, content))

这基本上就是列表里的内容。

for i in range(33,127):
    content.append('<div class="unicode-char">')
    char = cgi.escape(chr(i)).encode("ascii", "xmlcharrefreplace")
    content.append(char.decode())
    content.append('<div class="clearfix"></div><sub style="font-size: 12px">{sub_number}</sub>'.format(sub_number = i))
    content.append('</div>')

更新:我找到了一种打印列表的方法,但它只是把内容打印成一长行。我哪里做错了?我以为我设置成了 12 列。

1 个回答

0

如果content是一个Python列表,你可以用下面的方法把它转换成HTML:

''.join(content)

如果你想让列表中的每个元素都在不同的行上,可以使用:

'\n'.join(content)

如果在浏览器中看到的内容都在一行上,请给我们看看unicode-char的CSS样式。

撰写回答