我的python代码生成一个列表和一个字符串字典。
我想创建一个HTML页面并在表格中插入这些值。
我看过像yaptu这样的模板引擎,但我需要更简单的东西。
提前致谢。
答案 0 :(得分:0)
答案 1 :(得分:0)
也许你可以使用BeautifulSoup:
from bs4 import BeautifulSoup
html = '<html><body><table>'
for item in my_list:
html += '<tr>{0}</tr>'.format(str(item)) # this sort of thing
for k, v in my_dict.iteritems():
html += '<tr>{0}</tr>'.format(v) # or whatever
html += '</table></body></html>' # now you have the full html string
soup = BeautifulSoup(html)
html = soup.prettify() # this would actually auto close unclosed tags for you too
>>> print html
<html>
<body>
<table>
<rows>
</rows> # etc.
</table>
</body>
</html>
<{3}}上的
修改强>
关于你的评论
import os
os.chdir(path) # will change your cwd to path if you have a specific one in mind
将新生成的html文件保存到cwd:
with open('new.html', 'w') as f: # with will auto create file and close after writing
f.write(html)