python 结构化数据解析

# -*- coding: utf-8 -*-
# @Time    : 2018/8/31 14:32
# @Author  : cxa
# @File    : glomtest.py
# @Software: PyCharm
from glom import glom, Coalesce
import simplejson as sj
import pprint

jsonstr = """{"CERT_ID": "32143434", "CERT_NAME": "ssss", "PROD_ID": "CREDIT", "MP": "10086",
           "TRANS_INFO": "20180911", "DATA": [{"attributes": {"CR_PS_MC_LM24": 0.0, "CR_TR_TR_LM24": 0.0,
                                                                        "CD_AL_IS_LM24": 1.0, "CD_CC_AL_LM12": 0.0,
                                                                        "CR_DC_OGO2_LM12": 0.0, "CR_EX_EP_LM06": 0.0,
                                                                        "CR_CC_CS_LM03": 0.0}
                                                                        }, {"blacklist": {}}, {
                                                            "loan": {"record": [
                                                                {"matchType": "phone", "matchValue": "1204",
                                                                 "matchId": "CDGFHHSSSFAFRFRFRRFR",
                                                                 "classification": [{"M9": {
                                                                     "other": {"orgNums": 1, "loanAmount": null,
                                                                               "totalAmount": null, "repayAmount": null,
                                                                               "latestLoanTime": null}, "bank": null}},
                                                                                    {"M12": {"other": {"orgNums": 2,
                                                                                                       "loanAmount": null,
                                                                                                       "totalAmount": null,
                                                                                                       "repayAmount": null,
                                                                                                       "latestLoanTime": null},
                                                                                             "bank": null}}],
                                                                 "latestRepaySuccessTime": null}]}}, {"overdue": {}}]}"""
def get_last_str(jsonstr):
    new_dict = {}
    last_dict = dict(sj.loads(f'{jsonstr}'))
    spec = {
        'attributes': ('DATA', [Coalesce('attributes', default=None)]),
        'loan': ('DATA', [Coalesce(('loan.record', [
            Coalesce(('classification', [Coalesce(*([f'M{i}.other' for i in range(1, 999)]),*([f'M{i}.bankLoan' for i in range(1, 999)]), default=None)]),
                     default=None)]), default=None)])
    }
    gm = glom(last_dict.copy(), spec,default="出错了")
    for k, v in last_dict.items():
        if not isinstance(v, (list, dict)):
            new_dict.setdefault(k, v)
        else:
            for v2 in v:
                for k3, v3 in v2.items():
                    if isinstance(v3, dict):
                        if v3:
                            if "record" in v3.keys():
                                recordstr = glom(v3, ('record', [Coalesce('classification', default=None)],
                                                      [[Coalesce(*([f'M{i}.other' for i in range(1, 999)]),*([f'M{i}.bankLoan' for i in range(1, 999)]),
                                                                 default=None)]]))
                                for l in recordstr[0]:
                                    try:
                                      new_dict.update(
                                        {f"{lk}_1" if lk in new_dict.keys() else lk: lv for lk, lv in l.items()})
                                    except:
                                        pass
                            else:
                                new_dict.update(
                                    {f"{lk}_1" if lk in new_dict.keys() else lk: lv for lk, lv in v3.items()})

    return str(new_dict)

posted @ 2018-08-31 17:29  公众号python学习开发  阅读(878)  评论(0编辑  收藏  举报