将列表[]作为表格或png写入html输出

2024-10-02 08:15:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我读取了一个csv数据文件来提取144个变量,在这个例子中,我只使用了两个变量“D\u DIRECT\u MN”和“dirsbif\u cont”,后来我进行了计算,并将结果存储在一个列表对象[]

In [21]: data_3
Out[21]:

[['variable',
'D_DIRECT_MN',
'D_DIRECT_MN',
'D_DIRECT_MN',
'',
'',
'dirsbif_cont',
'dirsbif_cont',
'',
''],
['0%',
8.7137285157087145,
9.5731001671595735,
81.71317131713171,
'',
'',
82.67326732673267,
17.326732673267326,
'',
''],
['GINI',
0.12253479691887703,
'',
'',
'',
'',
0.32523477033143711,
'',
'',
'',
''],
['IV',
17.236250186661209,
'',
'',
'',
'',
50.627804561317987,
'',
'',
'',
'']]

我使用以下代码在excel工作表中编写:

row = 0
for col, data in enumerate(data_3):
    worksheet.write_column(row,col, data)

输出为:

variable             0%      GINI          IV
D_DIRECT_MN         4066    0,122534797  17,55178807
D_DIRECT_MN         4467        
D_DIRECT_MN         37703       
D_DIRECT_MN         426     


dirsbif_cont        38577   0,32523477   50,62780456
dirsbif_cont        8085

我如何在html中执行相同的操作:

在我的代码中,还使用此选项将数据添加到html格式:

def write_html(var, conpoda):
    add2 = """Variable: <b>"""+str(var)+"""</b>
        <br>
        <img  src='"""+conpoda+"""grafico_"""+var+"""_conPoda.png'  height='300'     width='380'>
    <br>
    <br>
    """
return add2 

此函数用于为每个值创建matplot图表,将.png保存在文件夹中,并将此.png插入到html中,在本例中,我的输出变量是两个,但通常我的输出变量是144。你知道吗

def grafph(tabla):
    f2 = open('Graficos Con Poda.html','w')
    msg2 = """<html>
              <head></head>
              <link rel="stylesheet"     href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
              <body><center><p><h1>Graficos <span class='badge badge-secondary'>Con Poda</span></h1></p></center>
          <br>
          <center>"""

#start plot
... ... ... #Code for plot variables
#end plot

    msg2 +=  write_html(var, conpoda)
msg2 += """</center>
    <br>
    </body>
    </html>"""

f2.write(msg2)
f2.close()

“var”包含变量名, “conpoda”包含一个方向文件夹

为了恢复,我需要相同的输出在excel表,但在html;我尝试了“n”次,什么也没有,我在谷歌搜索,但我没有找到一个解决方案,任何想法?你知道吗


Tags: brdataplotpngvarhtmlwritef2

热门问题