这是我的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)
这不起作用,因为线条不是正确的方式。