通过对行进行分组从输入矩阵创建 json 文件

Create json file from input matrix by grouping rows

给定下面的矩阵,我想创建 json 输出,它将遍历矩阵行并在组 (col2) 重复时追加项目 (col 1):

  matrix = [ 
        ['JAN', '1', 'abc'], 
        ['FEB', '7', 'xyz'], 
        ['FEB', '4', 'abc'], 
        ['MAR', '3', 'xyz'], 
        ['DEC','12', 'xyz']
      ]

如何创建 output/JSON 文件:

{
     "month": ["JAN","FEB","FEB","MAR","DEC"],
     "items": [
        { "group":"abc",
         "data":[1,4]
        },
        { "group":"xyz",
         "data":[7,3,12]
        }
      ]
    }

我试过转置 matrix/column 并构建一个字典,在其中我在 for 循环中追加项目,但我遗漏了一些东西,而且它变得很乱。应该是一个简单的方法。

这个节目:

from itertools import groupby
import json
matrix = [
    ['JAN',  '1', 'abc'],
    ['FEB',  '7', 'xyz'],
    ['FEB',  '4', 'abc'],
    ['MAR',  '3', 'xyz'],
    ['DEC', '12', 'xyz']
]
months = [x[0] for x in matrix]
items = [[x[2], int(x[1])] for x in matrix]
items = sorted(items, key=lambda x: x[0])
items = groupby(items, lambda x: x[0])
items = [{"group": k, "data": [x[1] for x in g]} for k, g in items]
result = {
    "month": months,
    "items": items
}
print (json.dumps(result, sort_keys=True, indent=4, separators=(',', ': ')))

产生这个输出:

{
    "items": [
        {
            "data": [
                1,
                4
            ],
            "group": "abc"
        },
        {
            "data": [
                7,
                3,
                12
            ],
            "group": "xyz"
        }
    ],
    "month": [
        "JAN",
        "FEB",
        "FEB",
        "MAR",
        "DEC"
    ]
}

最好使用 json 模块,但您可以在转换后使用标准文件写入:

with open('data.json', 'w') as data:
    data.write(str(my_dict))