繁体   English   中英

python 加入子词典

[英]python join sub-dictionaries

我正在尝试在 python 中加入子字典,以便有效的 json 组成我所拥有的是:

{
'ctx/language': 'en',
'ctx/territory': 'DE',
'composer_name': 'openEHR2study',
'Allergies': {
    'adverse_reaction-allergy': [{
            'reaction_event_summary': {
                'clinical_impact': [{
                        '|code': 'at0035'
                    }
                ]
            }
        }, {
            'recorded': ['2020-05-14T00:00:00.000Z']
        }, {
            'reaction_event_summary': {
                'certainty': [{
                        '|code': 'at0024'
                    }
                ]
            }
        }, {
            'substance_agent': ['s']
        }, {
            'reaction_reported': ['true']
        }, {
            'comment': ['c']
        }
    ]
}

}

我想要的是像这样加入“reaction_event_summary”:

{
'ctx/language': 'en',
'ctx/territory': 'DE',
'composer_name': 'openEHR2study',
'Allergies': {
    'adverse_reaction-allergy': [{
            'reaction_event_summary': {
                'clinical_impact': [{
                        '|code': 'at0035'
                    }
                ]
                 'certainty': [{
                        '|code': 'at0024'
                    }
                ]
            }
        }, {
            'recorded': ['2020-05-14T00:00:00.000Z']
        }, {
            'substance_agent': ['s']
        }, {
            'reaction_reported': ['true']
        }, {
            'comment': ['c']
        }
    ]
}

我不知道我应该如何遍历 json/list 和 dicts 来完成这项工作。

我做了一个粗略的尝试,请检查这是否有效。 我们正在尝试用另一个 deepcopy 迭代字典。


val = {
    'ctx/language': 'en',
    'ctx/territory': 'DE',
    'composer_name': 'openEHR2study',
    'Allergies': {
        'adverse_reaction-allergy': [
            {
                'reaction_event_summary': {
                    'clinical_impact': [{
                        '|code': 'at0035'
                    }
                    ]
                }
            }, {
                'recorded': ['2020-05-14T00:00:00.000Z']
            }, {
                'reaction_event_summary': {
                    'certainty': [{
                        '|code': 'at0024'
                    }
                    ]
                }
            }, {
                'substance_agent': ['s']
            }, {
                'reaction_reported': ['true']
            }, {
                'comment': ['c']
            }
        ]
    }
}

import copy

val1 = copy.deepcopy(val)
del val1['Allergies']['adverse_reaction-allergy']
val1['Allergies']['adverse_reaction-allergy'] = []
reaction_count = 0

for _d in val['Allergies']['adverse_reaction-allergy']:
    if _d.get('reaction_event_summary', False):
        if reaction_count < 1:
            reaction_count += 1
            val1['Allergies']['adverse_reaction-allergy'].append(
                {'reaction_event_summary': _d.get('reaction_event_summary')})
        else:
            print(_d.get('reaction_event_summary'))
            _temp = val1['Allergies']['adverse_reaction-allergy'][0]['reaction_event_summary']
            _temp['certainty'] = _d.get('reaction_event_summary',{}).get('certainty',{})
            val1['Allergies']['adverse_reaction-allergy'][0]['reaction_event_summary'] = _temp
    else:
        val1['Allergies']['adverse_reaction-allergy'].append(_d)
import json
print(json.dumps(val1, indent=2))

示例 output

{
  "ctx/language": "en",
  "ctx/territory": "DE",
  "composer_name": "openEHR2study",
  "Allergies": {
    "adverse_reaction-allergy": [
      {
        "reaction_event_summary": {
          "clinical_impact": [
            {
              "|code": "at0035"
            }
          ],
          "certainty": [
            {
              "|code": "at0024"
            }
          ]
        }
      },
      {
        "recorded": [
          "2020-05-14T00:00:00.000Z"
        ]
      },
      {
        "substance_agent": [
          "s"
        ]
      },
      {
        "reaction_reported": [
          "true"
        ]
      },
      {
        "comment": [
          "c"
        ]
      }
    ]
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM