【问题标题】:row wise duplicate data need to be eliminated(Rowspan) using Django Python需要使用 Django Python 消除逐行重复数据(Rowspan)
【发布时间】:2018-08-07 21:40:46
【问题描述】:

因为我在 python 中有一本字典。字典值的样子。

d= {'Report.thpl Team =FULL':{'cdp.c': 899, '_arp_fusen.c': 34, 'discovery.c': 34, 'subnet.c': 4},
 'P1.thpl Team = Over': {'discovery.c': 34, 'file23.c': 4, 'cdp/cdp.c': 937, '_fusen.c': 83},
 'P1_Report1.thpl': {'arp_fusen.c': 83, 'disynet.c': 34, 'routes.c': 2, 'routing.c': 937}}

我的桌子是这样的

|test                   | file_name          |coverage   |
**********************************************************
|Report.thpl Team =FULL | cdp.c              | 899       |
|Report.thpl Team =FULL | _arp_fusen.c       | 34        |
|Report.thpl Team =FULL | Discovery.c        | 34        |
|Report.thpl Team =FULL | subnet.c           | 4         |
|P1.thpl Team = Over    | discovery.c        | 34        |
|P1.thpl Team = Over    | file23.c           |  4        |
|P1.thpl Team = Over    | cdp/cdp.c          | 937       |
|P1.thpl Team = Over    | _fusen.c           | 83        |
|P1_Report1.thpl        | arp_fusen.c        | 83        |
|P1_Report1.thpl        | disynet.c          | 34        |
|P1_Report1.thpl        | routes.c           | 2         |
|P1_Report1.thpl        | routing.c          | 937       |
***********************************************************

article.html 中的代码

<table class="table table-hover" style="width:80%;" >
        <tr style="color:white;">
            <th>Test Case</th>
            <th>File Name</th>
            <th>Coverage</th>
        </tr>
       {% for key, value in d.items %}
        <tr>
            {% for k,v in value.items %}
             {% if forloop.parentloop.first %}
            <td rowspan="{{ key|length }}">{{ key }}</td>
            {% endif %}
            <td>{{ k }}</td>
           <td>{{ v }}</td>

             {% endfor %}

        </tr>
            {% endfor %}

         {% endif %}
    </table>

但我正在使用上面的代码获得逐行输出。因为我需要以下格式的输出。请帮帮我。

|test                   | file_name          |coverage   |
**********************************************************
|Report.thpl Team =FULL | cdp.c              | 899       |
|                       | _arp_fusen.c       | 34        |
|                       | Discovery.c        | 34        |
|                       | subnet.c           | 4         |
|P1.thpl Team = Over    | discovery.c        | 34        |
|                       | file23.c           |  4        |
|                       | cdp/cdp.c          | 937       |
|                       | _fusen.c           | 83        |
|P1_Report1.thpl        | arp_fusen.c        | 83        |
|                       | disynet.c          | 34        |
|                       | routes.c           | 2         |
|                       | routing.c          | 937       |
***********************************************************

因为我需要测试属性不要重复,它应该是不同的。请帮帮我。

【问题讨论】:

  • d 不是有效的 python dict..
  • 我刚刚将字典变量命名为 d
  • 'Report.thpl Team =FULL'后面不应该有冒号吗?
  • 抱歉,我在编辑时删除了冒号。这是一本有效的字典,请根据我的需要给出显示解决方案

标签: django python-2.7 django-templates django-views


【解决方案1】:

而不是普通的{{ key }} 你可以试试:

<td rowspan="{{ key|length }}">
    {% if forloop.first %}
        {{ key }}
     {% endif %}
</td>

    <table class="table table-hover" style="width:80%;" >
            <tr style="color:white;">
                <th>Test Case</th>
                <th>File Name</th>
                <th>Coverage</th>
            </tr>
           {% for key, value in d.items %}
             {% for k,v in value.items %}
               <tr>
                 <td> 
                    {% if forloop.parentloop.first %}
                      {{ key }}
                    {% endif %}
                 </td>
                 <td>{{ k }}</td>
                 <td>{{ v }}</td>
               </tr>
            {% endfor %}
         {% endfor %}

        </table>

【讨论】:

  • 如何在第二个for循环中根据'v'对dict value.items进行排序。请帮帮我。
  • {% for k,v in value.items|sort %}
  • 基于 'v' 的值我需要排序
  • 如果您从后端本身发送排序值会更好
  • 我正在尝试使用 so=sorted(d.iteritems(),key=operator.itemgetter(1)) 但它不起作用我需要根据表上的覆盖率进行排序
猜你喜欢
  • 2017-02-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-19
  • 1970-01-01
  • 2018-05-16
  • 2011-04-15
  • 1970-01-01
相关资源
最近更新 更多