簡體   English   中英

Python JSON格式化程序

[英]Python JSON formatter

我是python的新手。 剛開始嘗試使用小型程序。 我在以下地方看到了這個問題:

輸入的將是json,可以是以下格式:

'[ ["a","b","c"], [1,2,null], [null,3,4], [5,null,6] ]' 

要么 :

'[ { "a":1, "b":2 }, { "b":3, "c":4 }, { "c":6, "a":5 } ]'

我們應該將其轉換為:

output = '{ "a": [1,null,5], "b": [2,3,null], "c": [null,4,6] }'

到目前為止,我能想到的是檢查每個元素並附加到結果中。 有沒有簡單或更好的方法來在python中做到這一點。 請賜教。

使用collections module defaultdict ,方法是:

>>> import json
>>> s = '[ { "a":1, "b":2 }, { "b":3, "c":4 }, { "c":6, "a":5 } ]'
>>> 
>>> dic = json.loads(s)
>>> dic
[{'a': 1, 'b': 2}, {'b': 3, 'c': 4}, {'a': 5, 'c': 6}]
>>> kys = set(k for sub_d in d for k in sub_d) #creates uniques keys of dictionary d
>>> kys
{'a', 'b', 'c'}
>>>
>>> from collections import defaultdict
>>> my_dict = defaultdict(list)
>>> for d in dic:
        for k in kys:
            my_dict[k].append(d.get(k, None))
>>> my_dict
defaultdict(<class 'list'>, {'a': [1, None, 5], 'b': [2, 3, None], 'c': [None, 4, 6]})

至於其他情況:

>>> s = '[ ["a","b","c"], [1,2,null], [null,3,4], [5,null,6] ]'
>>> d = json.loads(s)
>>> d
[['a', 'b', 'c'], [1, 2, None], [None, 3, 4], [5, None, 6]]
>>> my_dict = dict(zip(d[0], zip(*d[1:])))
>>> my_dict
{'a': (1, None, 5), 'b': (2, 3, None), 'c': (None, 4, 6)}

如果您不希望將元組用作值,則:

>>> my_dict = defaultdict(list)
>>> for k,v in zip(d[0], zip(*d[1:])):
        my_dict[k].extend(v)

最后,將兩種情況歸為一個函數:

import json
from collections import defaultdict

def parse_data(data):
    data = json.loads(data) 
    my_dict = defaultdict(list)
    if isinstance(data[0], list):
        for k,v in zip(data[0], zip(*data[1:])):
            my_dict[k].extend(v)
    elif isinstance(data[0], dict):
        kys = set(k for sub_d in data for k in sub_d)
        for d in data:
            for k in kys:
                my_dict[k].append(d.get(k, None))
    return my_dict

s1 = '[ ["a","b","c"], [1,2,null], [null,3,4], [5,null,6] ]' 
d1 = parse_data(s1)
s2 = '[ { "a":1, "b":2 }, { "b":3, "c":4 }, { "c":6, "a":5 } ]'
d2 = parse_data(s2)

兩種格式都適用

import json


def convert_format(json_data):
    convert_to_py_obj = json.loads(json_data)
    new_format = dict()
    if isinstance(convert_to_py_obj, list) and len(convert_to_py_obj) > 1:
        index_0_keys = convert_to_py_obj[0]
        if isinstance(index_0_keys, list):
            for i, key in enumerate(index_0_keys):
                new_format[key] = []
                for sub_list in convert_to_py_obj[1:]:
                    new_format[key].append(sub_list[i])
        elif isinstance(index_0_keys, dict):
            for sub_dict in convert_to_py_obj:
                for key, val in sub_dict.iteritems():
                    if key in new_format:
                        new_format[key].append(val)
                    else:
                        new_format[key] = [val]
                none_keys = set(new_format.keys()) - set(sub_dict.keys())
                for key in none_keys:
                    new_format[key].append(None)
    return json.dumps(new_format)

使用Python 3嘗試一下:

def get_elements(json_txt):
import json
arr = json.loads(json_txt)
new = {}
list_of_keys = []
list_of_keys_from_dicts = [list(elem.keys()) for elem in arr]
# getting keys from json
for keys in list_of_keys_from_dicts:
    for key in keys:
        if key not in list_of_keys:
            list_of_keys.append(key)
for key in list_of_keys:
    new[key] = []
for element in arr:
    for key in list_of_keys:
        if key in element:
            new[key].append(element[key])
        else:
            new[key].append(None)
return json.dumps(new)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM