我正在编写一个脚本来将输出打印到html文件。我坚持我的输出格式。以下是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int addr_byte_0;
int addr_byte_1;
void scanner(int s)
{
char addr[200];
int i;
for (i = 0; i < 255; ++i)
{
sprintf(addr, "%d.%d.%d.%d", addr_byte_0, addr_byte_1, s, i);
printf("%s\n", addr);
}
}
int main()
{
int i;
//printf("Enter the first byte of the address: ");
scanf ("%d", &addr_byte_0);
//printf("Enter the second byte of the address: ");
scanf ("%d", &addr_byte_1);
for (i = 0; i < 255; ++i)
{
scanner(i);
}
return 0;
}
我希望我的输出是这样的:
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
答案 0 :(得分:0)
请改为尝试:
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。
答案 1 :(得分:0)
每次迭代时,您的循环都在创建新的<head></head><body><p></p></body></html><html>
。
此外,如果要删除段落之间的空格,请使用<style>p { margin: 0, !important; } </style>
。
您可以尝试使用此功能,该功能可以提供所需的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