Python--json解析数据

Python–json解析

from openpyxl import Workbook
import pandas as pd
import numpy as np
import json
import traceback
import os
from pandas.core.frame import DataFrame
# 读取csv
df_req = pd.read_csv(r"F:\报表\三方数据分析\税贷CAP规则调整\data_loan_infos.csv",header = 0, 
                     encoding='gb18030')
df_req = df_req.dropna(subset=['submit_result'])
#解析
df_req = df_req.sort_values(by='transport_id', ascending=False)
df_req.index = range(df_req.shape[0])
db_ins_debt_list = []
for i in range(df_req.shape[0]):
    transport_id = df_req.loc[i, 'transport_id']
    print( 'transport_id={}'.format( transport_id))
    try:
        submit_result = json.loads(df_req.loc[i, 'submit_result'])
        gmv_trend_weighted=submit_result.get('extra_info', {}).get('source_stats', {}).get('limit_details', {}).get('hangxin', {}).get('gmv_trend_weighted', {})
        gmv_sv_weighted=submit_result.get('extra_info', {}).get('source_stats', {}).get('limit_details', {}).get('hangxin', {}).get('gmv_sv_weighted', {})
        res = {'transport_id': transport_id,
                'gmv_trend_weighted':gmv_trend_weighted,
                   'gmv_sv_weighted':gmv_sv_weighted}
        db_ins_debt_list.append(res)
    except:
        print('transport_id={} throw EXCEPTION!'.format(transport_id))  
        traceback.print_exc() 
# 生成结果dataframe
df_debt_db = pd.DataFrame(db_ins_debt_list)
df_debt_db.to_csv(r"F:\报表\三方数据分析\税贷CAP规则调整\gmv_trend_weighted.csv",sep=',',index=False,header=True)
        

你可能感兴趣的:(Python)