Python - 将JSON键/值转换为键/值,其中value是数组

时间:2013-01-25 17:48:32

标签: python json

我有一个包含大量条目的JSON文件:

    {
    "area1": "California",
    "area2": "Sierra Eastside",
    "area3": "Bishop Area",
    "area4": "Volcanic Tablelands (Happy/Sad Boulders)",
    "area5": "Fish Slough Boulders",
    "grade": "V6 ",        
    "route": "The Orgasm",
    "type1": "Boulder",
    "type2": "NONE",
    "type3": "NONE",
    "type4": "NONE",
},

我想获取areatype条目并将其转换为数组:

   {
    "area": ["California","Sierra Eastside","Bishop Area","Volcanic Tablelands (Happy/Sad Boulders)","Fish Slough Boulders"]
    "grade": "V6 ",        
    "route": "The Orgasm",
    "type": ["Boulder","NONE","NONE","NONE"]
},

我有这个代码几乎可以工作:

json_data=open('../json/routes_test.json')
datas = json.load(json_data)
datas_arrays = []
area_keys = ['area1','area2','area3','area4','area5']
type_keys = ['type1','type2','type3','type4']

for data in datas:
    areaArray = []
    typeArray = []
    deleteArray = []
    for k, v in data.iteritems():
        for area_key in area_keys:
            if (k == area_key):
                areaArray.append(v)
                deleteArray.append(k)
        for type_key in type_keys:
            if (k == type_key):
                typeArray.append(v)
                deleteArray.append(k)
    for k in deleteArray:
        del data[k]
    data['area'] = areaArray
    data['type'] = typeArray
    datas_arrays.append(data)
    print datas_arrays
    print "********"

out = json.dumps(datas_arrays, sort_keys=True,indent=4, separators=(',', ': '))
print out
f_out= open('../json/toues_test_intoarrays.json', 'w')    
f_out.write(out)
f_out.close()   

问题是area数组全部乱序,而type数组是向后的,我不能拥有它。我发现奇怪的是,一个是无序的,一个是有序的但是后退。对我而言,似乎迭代应该确保它们按顺序排列。

2 个答案:

答案 0 :(得分:5)

Python词典有任意排序,它们没有排序。您想要使用预建的密钥列表:

with open('../json/routes_test.json') as json_data:
    datas = json.load(json_data)
    area_keys = ['area1','area2','area3','area4','area5']
    type_keys = ['type1','type2','type3','type4']

    for data in datas:
        data['area'] = [data[k] for k in area_keys]
        data['type'] = [data[k] for k in type_keys]
        for k in area_keys + type_keys:
            del data[k]

out = json.dumps(datas, sort_keys=True, indent=4, separators=(',', ': '))
print out
with open('../json/toues_test_intoarrays.json', 'w') as f_out:
    f_out.write(out)

可以就地更改词典。

您甚至可以从每个条目中确定areatype个键:

    for data in datas:
        keys = sorted(data.keys())

        area_keys = [k for k in keys if k.startswith('area')]
        data['area'] = [data[k] for k in area_keys]

        type_keys = [k for k in keys if k.startswith('type')]
        data['type'] = [data[k] for k in type_keys]

        for k in area_keys + type_keys:
            del data[k]

并且省略了包含'area1', 'area2'等硬编码列表的列表文字。

答案 1 :(得分:4)

按顺序迭代键。

for k, v in sorted(data.iteritems()):

一旦你超过9,这将失败,但它现在会做。