繁体   English   中英

Django 模板表缺少某些列的数据

[英]Django template table with missing data for some columns

好吧,我放弃了。 我一遍又一遍地搜索并丢失了一些看起来像答案的东西,所以我不得不问。

比如说,一个区域电网的数据,每个发电厂都有一个一天的生产计划,所有这些都是 go 作为 dicts 列表,每个小时都有一个计划。 数据还包括功耗预测,自然只有未来几个小时的数据。

[
 {'station_id':'Grid','plan_code':1000,'plan':{1:300,2:500,3:250,...,23:519,24:200}}
 {'station_id':'Plant1','plan_code':1001,'plan':{1:100,2:224,3:150,...,23:239,24:100}}
 {'station_id':'Plant2','plan_code':724,'plan':{1:200,2:226,3:100,...,23:240,24:100}} #every hour contains value
 {'station_id':'Consumption','plan_code':2003,'plan':{21:1600,22:1710,23:1250,24:1100}} #only few hours have data
] 

我试图用 Django 模板生成的是电网生产、消耗和平衡值表:

<table>
    <thead>
        <tr style="font-size: small">
            <th>Plan for</th>
            <th>Type</th>
            <th>01</th>
            <th>02</th>
            <th>03</th>
            ...
            <th>22</th>
            <th>23</th>
            <th>24</th>
        </tr>
    </thead>
    <tbody>
    {% for plan in plans %}
        <tr style="font-size: small">
            <td>{{ plan.station_id }}</td>
            <td>{{ plan.plan_code }}</td>
            {% for hour,val in plan.plan %}
               <td>{{ val }}</td>
            {%endfor%}
        </tr>
        {% endfor %}
    </tbody>
</table>

问题是:

  1. 如何确定显示计划的顺序?
  2. 如果该小时没有提供值,我如何用空单元格填充消费计划行?

请在这里帮助初学者。

想通了。

如果您的表格中缺少需要添加的单元格并且您尚未使用 pandas,您可以使用具有None值的键来完成字典。

    hours = range(1,25)

    for ob in a:
        for plan in a[ob]['plans']:
            if len(a[ob]['plans'][plan]) < 24:
                for h in hours:
                   a[ob]['plans'][plan][h] = a[ob]['plans'][plan].get(h)
                a[ob]['plans'][plan] = dict(sorted( a[ob]['plans'][plan].items()))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM