zoukankan      html  css  js  c++  java
  • 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)
    
    
  • 相关阅读:
    SQL server 数据库迁移到mysql
    MVC+EF之仓库模式
    MVC+EF之多表关联数据更新
    省市联动下拉菜单的实现
    MVC+EF之Attribute
    网页制作
    MVC5+EF6之排序、过滤、分页
    MVC5+EF6之分部视图(Partial View)
    Bookstap初步了解
    java8新特性学习三(Stream API)
  • 原文地址:https://www.cnblogs.com/c-x-a/p/9567041.html
Copyright © 2011-2022 走看看