将python输出打印到html文件
问题描述:
我正在编写脚本以将输出打印到html文件。我困在我的输出格式。下面是我的代码:将python输出打印到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
答
试试这个:
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被完全读取,将'message'写入myfile。
它只是给了我同样的事情。 – LearningEveryday
尝试一起使用break和\ n。在网页显示中需要br,在源显示中需要\ n。 \ n基本上意味着源显示中的新行。 –
顺便说一句,你使用上述代码而不是消息= j +','? –