web-dev-qa-db-ja.com

pythonヘッダーを指定してjsonをcsv / xlsxにネスト

以下のようなjsonを使用すると、オブジェクトのネストされた配列がさらにネストされた、最外部レベルのオブジェクトの配列です。

data = [{"a": [{"a1": [{"id0": [{"aa": [{"aaa": 97}, {"aab": "one"}], "ab": [{"aba": 97}, {"abb": ["one", "two"]}]}]}, {"id1": [{"aa": [{"aaa": 23}]}]}]}, {"a2": []}]}, {"b": [{"b1": [{"Common": [{"bb": [{"value": 4}]}]}]}]}]

これをcsv(または.xlsxファイル)に書き込む必要があります

これまでに試したことはありますか?

data_file = open('data_file.csv', 'w')
csv_writer = csv.writer(data_file)
for row in data:
  csv_writer.writerow(row)
data_file.close() 

これにより、空のファイル「data_file.csv」が作成されます。

また、CSVにヘッダーを追加する方法も教えてください。次のようにリストにヘッダーが保存されています

hdrs = ['Section', 'Subsection', 'pId', 'Group', 'Parameter', 'Value'] 

-これは5つのレベルのキーに対応します

予想されるCSV出力

+---------+------------+--------+-------+-----------+----------+
| Section | Subsection |  pId   | Group | Parameter |  Value   |
+---------+------------+--------+-------+-----------+----------+
| a       | a1         | id0    | aa    | aaa       | 97       |
| a       | a1         | id0    | aa    | aab       | one      |
| a       | a1         | id0    | ab    | aba       | 97       |
| a       | a1         | id0    | ab    | abb       | one, two |
| a       | a1         | id1    | aa    | aaa       | 23       |
| a       | a2         |        |       |           |          |
| b       | b1         | Common | bb    | value     | 4        |
+---------+------------+--------+-------+-----------+----------+

予想されるXLSX出力enter image description here

6
user3206440

次のコードは、期待される形式に従って、提供されたデータを解析できます。

from typing import List

def parse_recursive(dat)->List[List]:
    ret=[]
    if type(dat) is list:
        for item in dat:
            if type(item)==dict:
                for k in item:
                    #print(k, item[k], sep=" # ")#debug print
                    if item[k]==[]: #empty list
                        ret.append([k])
                    else:
                        for l in parse_recursive(item[k]):
                            #print(k,l,sep=" : ") #debug print
                            ret.append([k]+l) #always returns List of List
            else: #Right now only possibility is string eg. "one", "two"
                return [[",".join(dat)]]
    else: #can be int or string eg. 97, "23"
        return [[dat]]

    return ret


def write_to_csv(file_name:str, fields:List, row_data:List[List]):
    import csv
    with open(file_name, 'w') as csvfile:  
        # creating a csv writer object  
        csvwriter = csv.writer(csvfile)  
        # writing the fields  
        csvwriter.writerow(fields)  
        # writing the data rows  
        csvwriter.writerows(row_data)


if __name__=="__main__":
    org_data = [{"a": [
        {"a1": [
            {"id0": [
                {
                    "aa": [
                        {"aaa": 97},
                        {"aab": "one"}],
                    "ab": [
                        {"aba": 97},
                        {"abb": ["one", "two"]}
                        ]
                }
            ]
            },
            {"id1": [
                {"aa": [
                    {"aaa": 23}]}]}
            ]
        },
        {"a2": []}
        ]},
        {"b": [{"b1": [{"Common": [{"bb": [{"value": 4}]}]}]}]}]
    print(parse_recursive(org_data)) #Debug

    file_name="data_file.csv"
    fields=['Section', 'Subsection', 'pId', 'Group', 'Parameter', 'Value']
    write_to_csv(file_name, fields, parse_recursive(org_data))

parse_recursiveは、ルールに従って入力と出力のフォーマットから推測した任意の深度辞書を解析しようとします。

以下は、提供された入力に対するparse_recursiveの出力です-

mahorir@mahorir-Vostro-3446:~/Desktop$ python3 so.py 
[['a', 'a1', 'id0', 'aa', 'aaa', 97], ['a', 'a1', 'id0', 'aa', 'aab', 'one'], ['a', 'a1', 'id0', 'ab', 'aba', 97], ['a', 'a1', 'id0', 'ab', 'abb', 'one,two'], ['a', 'a1', 'id1', 'aa', 'aaa', 23], ['a', 'a2'], ['b', 'b1', 'Common', 'bb', 'value', 4]]

write_to_csvは、csvファイルに書き込む簡単な関数です。

3
Mahori

これは一種の楽しい問題でした...ここでの書式設定には本当に2つの問題があります。

  1. データはディクショナリーのリストであり、ディクショナリーが本当に欲しかったのです。例えば彼らは欲しかった {"foo": 1, "bar": 2}ですが、代わりに[{"foo": 1}, {"bar": 2}]

    a。私はここで判断していません。彼らがこれをした理由があるかもしれません。解析するのが少し面倒です。

  2. データは時々切り捨てられます。通常は5レベルの深さがある場合、あるポイントを超えるデータがない場合は、省略します。例えば'a2'あなたの例では。

そこで、これらの問題を解決するための2つの可能なアプローチを示します。

Pandas Way

このソリューションは、ここで説明した他のソリューションとは少し異なります。どう考えているか教えてください:

import pandas as pd
from copy import deepcopy

hdrs = ['Section', 'Subsection', 'pId', 'Group', 'Parameter', 'Value']

js = [{"a": [{"a1": [{"id0": [{"aa": [{"aaa": 97}, {"aab": "one"}],
                               "ab": [{"aba": 98}, {"abb": ["one", "two"]}]}]},
                     {"id1": [{"aa": [{"aaa": 23}]}]}
                    ]},
             {"a2": []}
            ]},
      {"b": [{"b1": [{"Common": [{"bb": [{"value": 4}]}]}]}]}]

def list_to_dict(lst):
    """convert a list of dicts as you have to a single dict

    The idea here is that you have a bunch of structures that look
    like [{x: ...}, {y: ...}] that should probably have been stored as
    {x:..., y:...}. So this function does that (but just one level in).
    
    Note:
    If there is a duplicate key in one of your dicts (meaning you have
    something like [{x:...},...,{x:...}]), then this function will overwrite
    it without warning!
    """
    d = {}
    for new_d in lst:
        d.update(new_d)
    return d

def recursive_parse(lst, levels):
    "Parse the nested json into a single pandas dataframe"
    name = levels.pop(0)  # I should have used a counter instead
    d = list_to_dict(lst)  # get a sensible dict instead of the list of dicts
    if len(levels) <= 1: # meaning there are no more levels to be parsed.
        if len(d) == 0:
            d = {'': ''} # to handle the uneven depths (e.g. think 'a2')
        return pd.Series(d, name=levels[-1])
    if len(d) == 0: # again to handle the uneven depths of json
        d = {'': []}
    # below is a list-comprehension to recursively parse the thing.
    d = {k: recursive_parse(v, deepcopy(levels)) for k, v in d.items()}
    return pd.concat(d)

def json_to_df(js, headers):
    "calls recursive_parse, and then adds the column names and whatnot"
    df = recursive_parse(js, deepcopy(headers))
    df.index.names = headers[:-1]
    df = df.reset_index()
    return df
df = json_to_df(js, hdrs)
display(df)

そして、出力はまさに必要なデータフレームです(ただし、インデックス列では必要ない場合があります)。後でcsvに書き込む場合は、次のようにします。

df.to_csv('path/to/desired/file.csv', index=False)

それは理にかなっていますか?

ミニマリストの方法

より良いバージョン(パンダを使用しない)...

import csv

hdrs = ['Section', 'Subsection', 'pId', 'Group', 'Parameter', 'Value']

js = [{"a": [{"a1": [{"id0": [{"aa": [{"aaa": 97}, {"aab": "one"}],
                               "ab": [{"aba": 98}, {"abb": ["one", "two"]}]}]},
                     {"id1": [{"aa": [{"aaa": 23}]}]}
                    ]},
             {"a2": []}
            ]},
      {"b": [{"b1": [{"Common": [{"bb": [{"value": 4}]}]}]}]}]

def list_of_dicts_to_lists(lst, n_levels=len(hdrs)):
    if n_levels == 1:
        if isinstance(lst, list):
            if len(lst) == 0: # we fill the shorter ones with empty lists
                lst = None # replacing them back to None
            else: # [1, 2] => "1,2"
                lst = ','.join(str(x) for x in lst if x is not None)
        return [[lst]] # the later ones are going to be lists of lists so let's start out that way to keep everything consistent.
    if len(lst) == 0:
        lst = [{None: []}] # filling with an empty list
    output = []
    for d in lst:
        for k, v in d.items():
            tmp = list_of_dicts_to_lists(v, n_levels - 1)
            for x in tmp:
                output.append([k] + x)
    return output

def to_csv(values, header, outfile):
    with open(outfile, 'w', newline='') as csv_file:
        # pretty much straight from the docs @
        # https://docs.python.org/3.7/library/csv.html
        csv_writer = csv.writer(csv_file, quoting=csv.QUOTE_MINIMAL)
        csv_writer.writerow(header)
        for line in values:
            csv_writer.writerow(line)
    return True

rows = list_of_dicts_to_lists(js)
to_csv(rows, hdrs, 'tmp.csv')

この解決策は、ここでの他の回答と非常によく似ていることがわかります。

3
Aaron