需要使用 Django Python 消除逐行重复数据(Rowspan)

Posted

技术标签:

【中文标题】需要使用 Django Python 消除逐行重复数据(Rowspan)【英文标题】:row wise duplicate data need to be eliminated(Rowspan) using Django Python 【发布时间】: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'后面不应该有冒号吗? 抱歉,我在编辑时删除了冒号。这是一本有效的字典,请根据我的需要给出显示解决方案 【参考方案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)) 但它不起作用我需要根据表上的覆盖率进行排序

以上是关于需要使用 Django Python 消除逐行重复数据(Rowspan)的主要内容,如果未能解决你的问题,请参考以下文章

逐行分析 Django 视图

从python中的文件逐行读取[重复]

在 django 模板 [python] 中使用 json.dump 或 pprint 逐行创建一个表

逐行读取TXT文件-Python [重复]

python使用逐行读取,出现空行,清楚空行方法

从文本文件中逐行提取数据并将其存储在python的列表中[重复]