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)
    
    
  • 相关阅读:
    【Log】【Log4j】【1】log4j日志的输出级别
    【Word&Excel】【1】更新Word的目录
    【服务器】【Windows】【5】让bat执行完后不关闭
    【Mybatis】【5】Oralce in 语句中当in(1,2,3...) 条件数量大于1000将会报错
    【JS插件】【1】移动端(微信等)使用 vConsole调试console
    【Oracle】【10】去除数据中的html标签
    【其他】【前端安全】【1】XSS攻击
    hdu 4433
    hdu 4435
    hdu 4752
  • 原文地址:https://www.cnblogs.com/c-x-a/p/9567041.html
Copyright © 2011-2022 走看看