在python中串联多个字典

时间:2018-12-29 18:32:10

标签: python-3.x dictionary

我需要将多个词典连接成一个最终的新词典。我在循环内创建字典,同时访问以下数据框组是高级步骤

以下是步骤

  1. 将数据框分成几组
  2. 通过组重复
  3. 使用组数据创建字典
  4. 在下一次迭代中,创建新的字典并将其连接到第一个字典
  5. 重复直到有分组为止

所以它应该是这样的

dataframe df groupby x

while there are groups in df 
   dict1 = to_dict(group)
   <some code>
   dict1 = dict1,dict2

输出结果

dict1 = [ {dict1 data},{dict2 data}]

现在是问题所在

下一次循环运行并说我正在将dict3串联起来,如下所示

dict1 = [[dict1],{dict3}]

并假设循环将花费更少的时间,它将成为更多带有标签的数组

我想要的输出如下所示

dict1 = [{dict1},{dict2},{dict3}........]

下面是我的代码,直到我试图连接字典的最后一点为止

import json
import pandas as pd


df = pd.read_csv('test2.csv')


i = 0

jiraTestCase = { "fields": {
   "project":
   {
      "key": "ayan testing"
   },
    "issuetype" : {"name":"Test"},
   "summary": "test",
   "customfield_11237": { "value": "temp" },
   "customfield_11101" : {"value": "temp"},
   "customfield_10028" : "temp",
   "customfield_10021": {"steps": []}
        }}


jiraTestCase2 = { "fields": {
   "project":
   {
      "key": "ayan testing"
   },
    "issuetype" : {"name":"Test"},
   "summary": "test",

   "customfield_11237": { "value": "temp" },
   "customfield_11101" : {"value": "temp"},
   "customfield_10028" : "temp",
   "customfield_10021": {"steps": []}
        }}




grouped = df.groupby('Summary')


for key, item in grouped:
tempgroup = grouped.get_group(key)
# print(tempgroup.iloc[0,0])
steps = tempgroup[['index', 'step', 'data', 'result']]
step_dict =steps.to_dict('records')
if i==0:
   jiraTestCase['fields']['customfield_10021']['steps'] = step_dict
   i=1

jiraTestCase2['fields']['customfield_10021']['steps'] = step_dict

jiraTestCase=jiraTestCase,jiraTestCase2


jirafinal = {}
jirafinal["issueUpdates"] = jiraTestCase


print(json.dumps(jirafinal))

0 个答案:

没有答案