如何将json转换为所需的格式

时间:2014-04-09 05:09:34

标签: python json google-visualization

我正在尝试使用谷歌图表可视化一些数据。我通过对公开数据发出查询来获取此数据。我在python中这样做。这是我的代码

query_string = 'SELECT state, count(*) FROM [{0}] GROUP by state;'.format(_DATABASE_NAME)
births = self.run_query(query_string, filename='data/states.json')
# rows = births[u'rows']
#logging.info(births)
ages= []

states.insert(0, ('Age','No of occurences'))
logging.info(json.encode(ages))
context = {"states": json.encode(ages)}

触发查询后,这就是我在JSON文件中获得的内容

[
    {
        "f0_": "6170247",
        "mother_age": "31"
    },
    {
        "f0_": "6876756",
        "mother_age": "30"
    },
    {
        "f0_": "8271245",
        "mother_age": "26"
    }
]

为了使其可视化,我将需要以下格式的数据 -

[
   ['Age', 'No of occurences'],
   ['31', '6170247'],
   ['30', '6876756'],
   .....
]

我该怎么做?我还意识到谷歌图表可能需要分类年龄?什么是最好的方法呢?在查询本身?

3 个答案:

答案 0 :(得分:1)

data = [
    {
        "f0_": "6170247",
        "mother_age": "31"
    },
    {
        "f0_": "6876756",
        "mother_age": "30"
    },
    {
        "f0_": "8271245",
        "mother_age": "26"
    }
]

编辑:正如@Matthew所说,如果你在json文件中有数据,你可以使用json模块加载这些数据。

import json
with open(<path_to_json>) as fname:
    data = json.load(fname)

转换数据

迭代字典列表,即data并将其添加到列表中

new_list = []
for item in data:
    new_list.append([data["mother_age"], data["f0_"]])

# new_list --> [['31', '6170247'], ['30', '6876756'], ['26', '8271245']]

对列表进行排序

您可以对此列表进行排序

new_list.sort(key=lambda sublist: sublist[0])
# new_list --> [['26', '8271245'], ['30', '6876756'], ['31', '6170247']]

或使用sorted函数创建新的排序列表,new_list不会被更改

final_list = sorted(new_list, key=lambda sublist: sublist[0])
# new_list --> [['31', '6170247'], ['30', '6876756'], ['26', '8271245']]
# final_list --> [['26', '8271245'], ['30', '6876756'], ['31', '6170247']]

或者您可以使用itemgetter代替sorted

from operator import itemgetter
final_list = sorted(new_list, key=itemgetter(0))

答案 1 :(得分:0)

使用json模块加载,然后迭代它以生成所需的结构:

import json

with open(<json-file>) as jfile:
    jdata = json.load(jfile)

data = [['Age', 'No of occurences']]
for d in jdata:
    data.append([d['mother_age'], d['f0_']])

在查询中对数据进行排序肯定会更好,但是使用json数据也很容易完成:

for d in sorted(jdata, key=lambda x: x['mother_age']):
    ...

答案 2 :(得分:0)

我会在Json上创建一个字典和一个循环来获得所需的格式,例如:

data = [
    {
        "f0_": "6170247",
        "mother_age": "31"
    },
    {
        "f0_": "6876756",
        "mother_age": "30"
    },
    {
        "f0_": "8271245",
        "mother_age": "26"
    }
]
well_formated_data = []
well_formated_data.append(['Age','No of occurences'])

for elem in data:
    new_array = []
    new_array.append(elem['mother_age'])
    new_array.append(elem['f0_'])
    well_formated_data.append(new_array)

我检查代码并且它有效,我得到了这个结果:

well_formated_data = [['Age', 'No of occurences'], ['31', '6170247'], ['30', '6876756'], ['26', '8271245']]
相关问题