想把字典写成excel ... dict值是嵌套列表..?

时间:2018-06-16 07:27:14

标签: python python-3.x python-2.7 pandas openpyxl

我有一本字典:

modernizr

希望它写入excel表。所以期待最终输出如下

res = {'err_1': [['ta1', 'ta2', 'ta3', 'ta4'],
                 ['tc1', 'tc2', 'tc3', 'tc4'],
                 ['2ta1', '2ta2', '2ta3'],
                 ['2tc1', '2tc2', '2tc3']],
       'err_2': [['2_ta1', '2_ta2', '2_ta3', '2_ta4'],
                 ['2_tc1', '2_tc2', '2_tc3', '2_tc4'],
                 ['2_2ta1', '2_2ta2', '2_2ta3'],
                 ['2_2tc1', '2_2tc2', '2_2tc3']]}

尝试使用熊猫和其他人......

err_1   ta1    tc1     2ta1    2tc1
        ta2    tc2     2ta2    2tc2
        ta3    tc3     2ta3    2tc3
        ta4    tc4      
err_2   2_ta1   2_tc1   2_2ta1  2_2tc1
        2_ta2   2_tc2   2_2ta2  2_2tc2
        2_ta3   2_tc3   2_2ta3  2_2tc3
        2_ta4   2_tc4

上面的代码是在单个单元格中写入整个dict值。我需要它不同的单元格... 请帮助我们......提前致谢....

1 个答案:

答案 0 :(得分:2)

您首先要问的是,您希望dict zip中的矩阵(嵌套列表)以换位形式书写。 itertools.zip_longest可用于转置嵌套列表;但是列表中缺少元素(即嵌套列表中的所有列表长度不相同)。因此,我们可以使用>>> from itertools import zip_longest >>> res2 = {k: [*zip_longest(*l, fillvalue='')] for k,v in res.items()} >>> res2 {'err_1': [('ta1', 'tc1', '2ta1', '2tc1'), ('ta2', 'tc2', '2ta2', '2tc2'), ('ta3', 'tc3', '2ta3', '2tc3'), ('ta4', 'tc4', '', '')], 'err_2': [('ta1', 'tc1', '2ta1', '2tc1'), ('ta2', 'tc2', '2ta2', '2tc2'), ('ta3', 'tc3', '2ta3', '2tc3'), ('ta4', 'tc4', '', '')]} >>> 为我们填充缺少的元素

Dataframe

现在您可以将dict转换为>>> import pandas as pd >>> d = {r + str(c): res2[r][c] for r in res2 for c in range(len(res2[r]))} >>> df = pd.DataFrame.from_dict(d, orient='index') >>> df 0 1 2 3 err_10 ta1 tc1 2ta1 2tc1 err_11 ta2 tc2 2ta2 2tc2 err_12 ta3 tc3 2ta3 2tc3 err_13 ta4 tc4 err_20 ta1 tc1 2ta1 2tc1 err_21 ta2 tc2 2ta2 2tc2 err_22 ta3 tc3 2ta3 2tc3 err_23 ta4 tc4 >>> ,如下所示

>>> writer = pd.ExcelWriter('output.xlsx')
>>> df.to_excel(writer, 'Sheet1', index_label=False)
>>> writer.save()

最后,您可以将其写入excel文件,如您已经提到的那样

   <config evaluator="task-type" condition="bpm:startTask">
  <forms>
     **<form id="workflow-details">
        <field-visibility>
           <show id="bpm:sendEMailNotifications" />
           <show id="packageItems" />
        </field-visibility>
        <appearance>
           <set id="" appearance="title" label-id="workflow.set.workflow.more_info" />
           <set id="items" appearance="title" label-id="workflow.set.items" />
           <field id="packageItems" set="items" />
        </appearance>
     </form>**

     <form>
        <field-visibility>
           <show id="message" />
           <show id="taskOwner" />
           <show id="bpm:workflowPriority" />
           <show id="bpm:workflowDueDate" />
           <show id="bpm:taskId" />
           <show id="bpm:status" />
           <show id="packageItems" />
           <show id="bpm:sendEMailNotifications" />
        </field-visibility>
        <appearance>
           <set id="" appearance="title" label-id="workflow.set.task.info" />
           <set id="info" appearance="" template="/org/alfresco/components/form/3-column-set.ftl" />
           <set id="progress" appearance="title" label-id="workflow.set.task.progress" />
           <set id="items" appearance="title" label-id="workflow.set.items" />
           <set id="other" appearance="title" label-id="workflow.set.other" />

           <field id="message">
              <control template="/org/alfresco/components/form/controls/info.ftl" />
           </field>
           <field id="taskOwner" set="info" />
           <field id="bpm:taskId" set="info">
              <control template="/org/alfresco/components/form/controls/info.ftl" />
           </field>
           <field id="bpm:workflowPriority" label-id="workflow.field.priority" set="info" read-only="true">
              <control template="/org/alfresco/components/form/controls/workflow/priority.ftl" />
           </field>
           <field id="bpm:workflowDueDate" set="info" label-id="workflow.field.due">
              <control template="/org/alfresco/components/form/controls/info.ftl" />
           </field>
           <field id="bpm:status" set="progress" />
           <field id="bpm:sendEMailNotifications" set="other" />
           <field id="packageItems" set="items" />
        </appearance>
     </form>
  </forms>