python json解析未知个数_当密钥未知时,如何使用python解析json?

这是我的JSON示例:

text = {"rates":{

"AT":{

"country_name":"Austria",

"standard_rate":20,

"reduced_rates":{

"food":10,

"books":10

}

}

}}

现在“at”是国家代码。

它不是固定的

. 它也可以是GB、IT等。

我想分析这个JSON并从它的列中获取如下内容:

rates.AT rates.AT.country_name rates.AT.reducted_rates.food

AT Austria 10

也可以重命名为:

code country_name food

AT Austria 10

比如说,在另一次跑步中,我有:

text = {"rates":{

"IT":{

"country_name":"Italy",

"standard_rate":20,

"reduced_rates":{

"food":13,

"books":11

}

}

}}

然后需要:

rates.IT rates.IT.country_name rates.IT.reducted_rates.food

IT Italy 13

也可以重命名为:

code country_name food

IT Italy 13

我该怎么做?

编辑:

如果可能的话,使用@gphilo answer,我希望能得到熊猫数据帧的数据。

像什么?

df = pd.DataFrame()

for k,item in dic['rates'].items(): # use iteritems() if you're on Python 2

line = '{};{};{}'.format(k, item['country_name'], item['reduced_rates']['food'])

df = df.append(line, ignore_index=True)

这不起作用,因为线条不是正确的方式。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值