我正在复杂查询上执行计算并获取字典作为最终输出,现在希望以html格式打印该字典,而不用硬编码列名,因为它是动态的。Python动态词典+打印html输出
如果列不存在,则在报告中添加0。
data = {'Author1': defaultdict(<class 'dict'>,
{'Microsoft': 248,
'Ubuntu': 24,
'IOS': 24,
'Solaris': 24,
'C': 248}),
'Author2': defaultdict(<class 'dict'>,
{'Microsoft': 38,
'Ubuntu': 38,
'IOS': 38,
'Go': 38,
'C': 38}),
'Author3': defaultdict(<class 'dict'>,
{'Microsoft': 2,
'IOS': 2,
'Go': 2,
'C': 2})}
输出
Name Microsoft Ubuntu IOS Go Solaris C
Author1 248 24 24 0 24 248
Author2 38 38 38 38 0 38
Author3 2 0 2 2 0 2
代码:一个词典,这需要除去内部默认字典的
html = '<table><tr><th>' + '</th><th>'.join(data.keys()) + '</th></tr>'
for row in zip(*data.values()):
html += '<tr><td>' + '</td><td>'.join(row) + '</td></tr>'
html += '</table>'
print(html)
此代码错误出BEC。
如果你得到一个错误,你必须显示完整的错误信息。无论如何,请记住,一个字典不会保存顺序,我不认为你需要的是'defaultdic(dict,...)' –