Python 解析嵌套json

# !/usr/bin python
# -*- coding: utf-8 -*-


from __future__ import print_function
import sys
import time, datetime
import json
import re




for line in sys.stdin:
    try:
        json_2 = json.loads(line, encoding='utf8')
        for k,v in json_2.items():
	        if k == 'feed_list':
		        for i in range(len(v)):
		            nid =-1
		            q_ratio = -1
		            rec_src = -1
		            vt_ctr_fea =-1
		            predictor_q_score = -1
		            keys=v[i].keys()
		            ext=None
		            ext_keys=None
                    if 'ext' in keys:
			            ext= v[i].get('ext')
		                ext_keys=ext.keys()
		            if ext ==None:continue
		            if 'q_ratio' in ext_keys:
		                q_ratio= ext.get('q_ratio')
		            if 'rec_src'  in ext_keys:
		        	    rec_src=ext.get('rec_src')[0]
		        	#if rec_src not in xinmi:continue
		            if 'vt_ctr_fea' in ext_keys:
		        	    vt_ctr_fea= ext.get('vt_ctr_fea')
		            if 'predictor_q_score' in ext_keys:
		        	    predictor_q_score = ext.get('predictor_q_score')
		            if 'aq_value' in ext_keys:
		        	    aq_value = ext.get('aq_value')
	                print(cuid, nid, rec_src, predictor_q_score, vt_ctr_fea, q_ratio, aq_value,sep='\t')
    except Exception as e:
        pass
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值