Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将python输出打印到html文件_Html_Python 3.x - Fatal编程技术网

将python输出打印到html文件

将python输出打印到html文件,html,python-3.x,Html,Python 3.x,我正在编写一个脚本,将输出打印到html文件中。我被输出的格式卡住了。下面是我的代码: def printTohtml(Alist): myfile = open('zip_files.html', 'w') html = """<html> <head></head> <body><p></p>{htmlText}</body> </html>"""

我正在编写一个脚本,将输出打印到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 
请尝试以下方法:

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中的每个元素,您需要将它们存储在“message”中。一旦每个newL被完全读取,将“消息”写入myfile。

尝试以下操作:

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中的每个元素,您需要将它们存储在“message”中。一旦每个newL被完全读取,将“消息”写入myfile。

在每次迭代中,您的循环都会创建新的

另外,如果要删除段落之间的空格,请使用
p{margin:0,!important;}

您可以尝试此函数,它提供所需的
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')
该页面显示以下输出:

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

在每次迭代中,循环都会创建新的

另外,如果要删除段落之间的空格,请使用
p{margin:0,!important;}

您可以尝试此函数,它提供所需的
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')
该页面显示以下输出:

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

它给了我同样的东西。请尝试同时使用break和\n。在网页显示中需要br,而在源显示中需要\n\n基本上是指源代码显示中的新行。顺便说一句,您是否使用上述代码而不是message=j+','?我想我的问题是如何删除该新行\n。例如,在Python3中,print(j+',,end='')将删除新行,这样当我们打印列表的元素时,它们都可以在一行上。我的问题是,如果我想在HTML文件上输出,我该如何做同样的事情?您能将链接附加到输出的屏幕截图上以便于理解吗?它只是给了我同样的东西。请尝试同时使用break和\n。在网页显示中需要br,而在源显示中需要\n\n基本上是指源代码显示中的新行。顺便说一句,您是否使用上述代码而不是message=j+','?我想我的问题是如何删除该新行\n。例如,在Python3中,print(j+',,end='')将删除新行,这样当我们打印列表的元素时,它们都可以在一行上。我的问题是,如果我想把我的输出放在HTML文件上,我该如何做同样的事情呢?你能把一个链接附加到你的输出的屏幕截图上以便于理解吗?