我有多个词典,需要连接并最后添加一个父级。我该如何实现
dict1 = { "project":"test1", "summary": "A Test","issuetype": {
"name": "Test"
}}
dict2 = { "project":"test2", "summary": "B Test","issuetype": {
"name": "Test"
}}
我需要它的格式
dictParent = {fields: {
{ "project":"test1", "summary": "A Test","issuetype": {
"name": "Test"
}}
{ "project":"test2", "summary": "B Test","issuetype": {
"name": "Test"
}}
}}
}
我使用了以下代码
dictParent = {}
dictParent["fields"] = dict1, dict2
这种工作方式的问题是它作为数组出现,但是我希望字段成为父对象而不是数组
{fields: [
{ "project":"test1", "summary": "A Test","issuetype": {
"name": "Test"
}}
{ "project":"test2", "summary": "B Test","issuetype": {
"name": "Test"
}}
}}
]
答案 0 :(得分:0)
此
dictParent = {fields: { { "project":"test1", "summary": "A Test","issuetype": { "name": "Test" }} { "project":"test2", "summary": "B Test","issuetype": { "name": "Test" }} }} }
无效的python:
key:value
对-您的第二本字典没有没有密钥 fields
不是字符串,而是可变的说明符-我的猜测是您错过了它周围的'fields'
您可以这样存储数据(内容缩短到不超过80个字符/行):
dictParent = {"fields": [ {"project":"t1", "summary":"A", "issuetype": {"name": "Test"}},
{"project":"t2", "summary":"B", "issuetype": {"name": "Test"}}] }
wich将使dictParent
成为具有1个键('fields'
)的字典,该字典具有作为值的字典列表。
您可以像这样从dict1和dict2创建它:
dictParent = { "fields": [dict1, dict2] }
或者您可以指定其他键(内容缩短到不超过80个字符/行):
dictParent = {"fields_1": {"project":"t1", "summary":"A", "issuetype": {"name": "Test"}},
"fields_2": {"project":"t2", "summary":"B", "issuetype": {"name": "Test"}}}
这将使dictParent
成为字典词典:
dictParent = { "fields_1":dict1, "fields_2":dict2] }