如何将文本文件转换为python中的嵌套json?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (70)

我的数据如下所示。这是输入文件:

storeId,id,itemId,description
123,1,101,item_1
123,1,102,item_2
123,1,103,item_3
123,2,201,item_4
123,2,202,item_5

我想使用Python解析它并编写等效的JSON,以便以下列格式将信息重写到文件中:

[{
    "storeId": 123,
    "itemType": [{
        "id": 1,
        "items": [{
            "itemId": 101,
            "description": "item_1"
        }, {
            "itemId": 102,
            "description": "item_2"
        }, {
            "itemId": 103,
            "description": "item_3"
        }]
    }, {
        "id": 2,
        "images": [{
            "itemId": 201,
            "description": "item_4"
        }, {
            "itemId": 202,
            "description": "item_5"
        }]
    }]
}]
    enter code here

我该如何做到这一点?

提问于
用户回答回答于

你可以用itertools.groupby:

import itertools, csv
data = [[int(b) if b.isdigit() else b for b in i] for i in csv.reader(open('filename.csv'))][1:]
headers = ['storeId', 'itemType', 'id', 'items', 'itemId', 'description']
def create_structure(d, headers = headers):
  c = [[a, list(b)] for a, b in itertools.groupby(sorted(d, key=lambda x:x[0]), key=lambda x:x[0])]
  return [{headers[0]:a, headers[1]:create_structure([i for _, *i in b], headers = headers[2:]) if len(headers[2:]) > 2 else [dict(zip(headers[2:], i)) for _, *i in b]} for a, b in c]

import json
print(json.dumps(create_structure(data), indent = 4))

输出:

[
  {
    "storeId": 123,
    "itemType": [
        {
            "id": 1,
            "items": [
                {
                    "itemId": 101,
                    "description": "item_1"
                },
                {
                    "itemId": 102,
                    "description": "item_2"
                },
                {
                    "itemId": 103,
                    "description": "item_3"
                }
            ]
        },
        {
            "id": 2,
            "items": [
                {
                    "itemId": 201,
                    "description": "item_4"
                },
                {
                    "itemId": 202,
                    "description": "item_5"
                }
            ]
        }
     ]
   }
]

扫码关注云+社区

领取腾讯云代金券