2017-02-22 115 views
0

我正在复杂查询上执行计算并获取字典作为最终输出,现在希望以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。

+0

如果你得到一个错误,你必须显示完整的错误信息。无论如何,请记住,一个字典不会保存顺序,我不认为你需要的是'defaultdic(dict,...)' –

回答

2

怎么样使用pandas它所有的job

import pandas 
df = pandas.DataFrame.from_dict(data) 
html = """<!DOCTYPE html> 
<html> 
    <body> 
{body} 
    </body> 
</html> 
""" 
with open("test.html","w") as file: 
    file.write(html.format(body=df.to_html(na_rep="0"))) 

这个结果在该表

enter image description here

使用.T以调换表

with open("test2.html","w") as file: 
    file.write(html.format(body=df.T.to_html(na_rep="0"))) 

,并导致

enter image description here


没有大熊猫,他们我会去这样

html = """<!DOCTYPE html> 
<html> 
    <body> 
{body} 
    </body> 
</html> 
""" 

table=""" 
<table border="1" class="dataframe"> 
    <thead> 
{thead} 
    </thead> 
    <tbody> 
{tbody} 
    </tbody> 
</table> 
""" 

thead=""" 
<tr style="text-align: right;"> 
{th} 
</tr> 
""" 
th="""<th>{}</th>\n""" 
td="""<td>{}</td>\n""" 
tr="""<tr>{}</tr>\n""" 

def manual_table(data, colums): 
    head = thead.format(th= "".join(map(th.format,["Name"]+colums))) 
    pieces=[] 
    for autor, value in data.items(): 
     temp = [autor] 
     temp.extend(value.get(c,0) for c in colums) 
     pieces.append(tr.format("".join(td.format(x) for x in temp))) 
    body = "\n".join(pieces) 
    return table.format(thead=head, tbody=body) 

colums="Microsoft Ubuntu IOS Go Solaris C".split() 

with open("test3.html","w") as file: 
    file.write(html.format(body=manual_table(data, colums))) 

首先我建了一些模板,所以我得到的东西,我可以在读取结束,然后我会相应地填写这些模板,并按照我通过colums变量强加的顺序进行填充。

这个结果

enter image description here


一个与您的代码的问题是,你不按照正确的顺序来读取值,对于一个部分解决方案是

colums="Microsoft Ubuntu IOS Go Solaris C".split() 
for row in data.values(): 
    table += '<tr><td>' + '</td><td>'.join(str(row.get(c,0)) for c in colums) + '</td></tr>' 

如果您不想硬编码列的名称,但从数据中获取它们,那么您可以这样做:

>>> {k for v in data.values() for k in v } 
{'C', 'IOS', 'Solaris', 'Ubuntu', 'Microsoft', 'Go'} 
>>> 

约在评论你的问题,是的,你可以和很容易sum,例如像

>>> df 
      Author1 Author2 Author3 
C   248.0  38.0  2.0 
Go    NaN  38.0  2.0 
IOS   24.0  38.0  2.0 
Microsoft 248.0  38.0  2.0 
Solaris  24.0  NaN  NaN 
Ubuntu  24.0  38.0  NaN 
>>> df["Total"] = df.T.sum() 
>>> df 
      Author1 Author2 Author3 Total 
C   248.0  38.0  2.0 288.0 
Go    NaN  38.0  2.0 40.0 
IOS   24.0  38.0  2.0 64.0 
Microsoft 248.0  38.0  2.0 288.0 
Solaris  24.0  NaN  NaN 24.0 
Ubuntu  24.0  38.0  NaN 62.0 

,直到你的愿望的结果只是玩它,并检查documentation所以你知道它所有的玩具

+0

哇,'na_rep ='0''自动将空字符转换为0!干净的输出尽管输入不佳...尼斯 –

+0

使用熊猫看起来不错!是否有可能使用熊猫在表中添加“总计”列,或者是否需要在传递值之前计算自己? –

+0

@VinodHC是的,检查更新 – Copperfield