【问题标题】:Print a python output to an html file将 python 输出打印到 html 文件
【发布时间】:2025-11-02 22:10:01
【问题描述】:

我正在编写一个脚本来将输出打印到一个 html 文件。我被困在我的输出格式上。以下是我的代码:

def printTohtml(Alist):
    myfile = open('zip_files.html', 'w')
    html =  """<html>
    <head></head>
    <body><p></p>{htmlText}</body>
    </html>"""

    title = "Study - User - zip file -  Last date modified"
    myfile.write(html.format(htmlText = title))
    for newL in Alist:
        for j in newL:
            if j == newL[-1]:
                myfile.write(html.format(htmlText=j))
            else:
                message = j + ', '
                myfile.write(html.format(htmlText = message))
    myfile.close()

Alist =  [['123', 'user1', 'New Compressed (zipped) Folder.zip', '05-24-17'],
['123', 'user2', 'Iam.zip', '05-19-17'], ['abcd', 'Letsee.zip', '05-22-17'],
['Here', 'whichTwo.zip', '06-01-17']]

printTohtml(Alist)

我希望我的输出是这样的:

Study - User - zip file -  Last date modified
123, user1, New Compressed (zipped) Folder.zip, 05-24-17
123, user2, Iam.zip, 05-19-17
abcd, Letsee.zip, 05-22-17
Here, whichTwo.zip, 06-01-17

但是我的代码给了我自己的一切。谁能帮帮我?

提前感谢您的帮助!

我的输出:

Study - User - zip file - Last date modified 

123, 

user1, 

New Compressed (zipped) Folder.zip, 

05-24-17 

123, 

user2, 

Iam.zip, 

05-19-17 

abcd, 

Letsee.zip, 

05-22-17 

Here, 

whichTwo.zip, 

06-01-17 

【问题讨论】:

    标签: html python-3.x


    【解决方案1】:

    试试这个:

    for newL in Alist:
            for j in newL:
                if j == newL[-1]:
                    myfile.write(html.format(htmlText=j))
                else:
                    message = message + ', ' + j
            myfile.write(html.format(htmlText = message))
    

    对于 newL 中的每个元素,您需要将它们存储在“消息”中。完全读取每个 newL 后,将“消息”写入 myfile。

    【讨论】:

    • 它只是给了我同样的东西。
    • 尝试同时使用 break 和 \n。网页显示需要br,源码显示需要\n。 \n 基本上意味着源显示中的新行。
    • 顺便说一句,您是否使用上述代码而不是 message = j + ', ' ?
    • 我想我的问题是如何删除新行\n。例如,在 python 3 中, print(j + ', ', end = '') 会在我们打印列表的元素时删除新行,它们都可以在一行上。我的问题是,如果我想在 HTML 文件上输出,我该怎么做?
    • 您能否将链接附加到您的输出屏幕截图以便于理解?
    【解决方案2】:

    每次迭代,您的循环都会创建新的&lt;head&gt;&lt;/head&gt;&lt;body&gt;&lt;p&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;&lt;html&gt;

    另外,如果您想删除段落之间的空格,请使用&lt;style&gt;p { margin: 0, !important; } &lt;/style&gt;

    你可以试试这个函数,它会给出你想要的HTML 输出。

    def printTohtml(Alist, htmlfile):    
        html =  "<html>\n<head></head>\n<style>p { margin: 0 !important; }</style>\n<body>\n"
    
        title = "Study - User - zip file -  Last date modified"
        html += '\n<p>' + title + '</p>\n'
    
        for line in Alist:
            para = '<p>' + ', '.join(line) + '</p>\n'
            html += para
    
        with open(htmlfile, 'w') as f:
            f.write(html + "\n</body>\n</html>")
    
    Alist =  [['123', 'user1', 'New Compressed (zipped) Folder.zip', '05-24-17'],
    ['123', 'user2', 'Iam.zip', '05-19-17'], ['abcd', 'Letsee.zip', '05-22-17'],
    ['Here', 'whichTwo.zip', '06-01-17']]
    
    printTohtml(Alist, 'zip_files.html')
    

    写入zip_files.html 的标记是:

    <html>
    <head></head>
    <style>p { margin: 0 !important; }</style>
    <body>
    
    <p>Study - User - zip file -  Last date modified</p>
    <p>123, user1, New Compressed (zipped) Folder.zip, 05-24-17</p>
    <p>123, user2, Iam.zip, 05-19-17</p>
    <p>abcd, Letsee.zip, 05-22-17</p>
    <p>Here, whichTwo.zip, 06-01-17</p>
    
    </body>
    </html>
    

    页面显示以下输出:

    Study - User - zip file - Last date modified    
    123, user1, New Compressed (zipped) Folder.zip, 05-24-17    
    123, user2, Iam.zip, 05-19-17    
    abcd, Letsee.zip, 05-22-17    
    Here, whichTwo.zip, 06-01-17
    

    【讨论】: