I am performing computation on complex query and getting below dictionary as final output and now want to print that dict in html format without hardcoding column names as it is dynamic.
If column is not there then add 0 in the report.
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})}
Output
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
code:
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)
This code errors out bec of default dict inside a dictionary, which needs to be removed.



defaultdic(dict, ...)