python结构化数据_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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值