import sqlite3,os
db=r'F:\workspace\china\data\china.sqlite'
con=sqlite3.connect(db)
cur=con.cursor()
res=cur.execute('select 代码,所属行业,注册资本,雇员人数,管理人员人数 from profile limit 20').fetchall()
for row in res:
print(row)(“600000”,“银行”,“187亿”,39340.0,30.0)
(“600004”,“民航机场”,“11.5亿”,4499.0,23.0)
(“600005”,“钢铁行业”,“101个亿”,38857.0,24.0)
('600006','汽车行业','20.0亿',10290.0,20.0)
(“600007”,“房地产”,“10.1亿”,2332.0,19.0)
('600008','公用事业','22.0亿',6515.0,20.0)
(“600009”,“民航机场”,“19.3亿”,5472.0,18.0)
('600010','钢铁行业','80.0亿',31389.0,19.0)
(“600011”,“电力行业”,“141亿”,37729.0,29.0)
('600012','高速公路','16.6亿',2106.0,14.0)
('600015','银行','89.0亿',25200.0,34.0)
('600016','银行','340亿',54927.0,32.0)
(“600017”,“港口水运”,“30.8亿”,5340.0,21.0)
('600018','港口水运','228亿',19842.0,20.0)
('600019','钢铁行业','165亿',37487.0,23.0)
(“600020”,“高速公路”,“22.5亿”,2959.0,32.0)
(“600021”,“电力行业”,“21.4亿”,6673.0,22.0)
('600022','钢铁行业','64.4亿',31738.0,20.0)
('600023','电力行业','118亿',10142.0,14.0)
('600026','港口水运','34.0亿',7536.0,21.0)
现在,我想在html表中显示输出。
html_str='<table border=1>'
for row in res:
html_str=html_str+'<tr>'
for col in row:
html_str=html_str+'<td>'+str(col)
html_str=html_str+'</td>'
html_str=html_str+'</tr>'
html_str=html_str+'</table>'
myhtml=open('f:\\test.html','w')
myhtml.write(html_str)
myhtml.close()
os.system('"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" f:\\test.html')我想改进我的代码
1.简化html文件的创建。
2.使用
os.system('"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" f:\\test.html')
我怎样才能更优雅地用寺庙jinjia2来显示它呢?
发布于 2014-09-24 03:46:54
用jinja2.Template制作模板
import jinja2
template = jinja2.Template('''
<table border=1>
{% for row in res %}
<tr>
{% for col in row -%}
<td>{{ col|replace('\s', ':1,7>') }}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
''')使用jinja2.Template.render (传递res )呈现模板,然后将其写入文件:
with open('test.html', 'w') as f:
f.write(template.render(res=res))若要在浏览器中打开文件,请使用webbrowser.open (它将使用默认的web浏览器):
import webbrowser
webbrowser.open('test.html')https://stackoverflow.com/questions/26008093
复制相似问题