簡體   English   中英

Python - CSV 到 JSON - 嵌套

[英]Python - CSV to JSON - nested

我有這個 csv:

product_id, width, height
14866, 200, 200
14866, 300, 300

我正在使用 csv 導入和 json 嘗試創建 json 以發出 Z8A5DA52ED1264477D35AZE70C0A8 請求。

這就是我的 python 現在的樣子:

import csv
import json


json_dict = {}
results = []
with open('sizes.csv',encoding='utf-8-sig') as f:
   for row in csv.DictReader(f,):
        results.append(
            {'id': int(row['product_id']),
             'product': 
             {'product_creative_size_attributes':[{'width':str(row['width']),'height': str(row['height'])}]}})

json_dict["sizes"] = results

output_json = print(json.dumps(json_dict,indent=4))

這導致了這個 json:

{
    "sizes": [
        {
            "id": 14866,
            "product": {
                "product_creative_size_attributes": [
                    {
                        "width": "200",
                        "height": "200"
                    }
                ]
            }
        },
        {
            "id": 14866,
            "product": {
                "gam_product_creative_size_attributes": [
                    {
                        "width": "300",
                        "height": "300"
                    }
                ]
            }
        }
    ]
}

我試圖實現的 json 是將相同 product_id 的大小嵌套如下:

{
    "sizes": [
        {
            "id": 14866,
            "product": {
                "product_creative_size_attributes": [
                    {
                        "width": "200",
                        "height": "200"
                    },
                    {
                        "width": "300",
                        "height": "300"
                    }
                ]
            }
        }
    ]
}

我將首先使用列表的collections.defaultdict按產品 ID 分組,然后組裝您的最終字典並將其序列化為 JSON。

演示:

from csv import DictReader
from collections import defaultdict
from json import dumps

result = {}
products = defaultdict(list)

with open("sizes.csv") as f:

    # make sure headers don't have whitespace, since they are used as dict keys later
    header = [h.strip() for h in next(f).split(",")]

    for row in DictReader(f, fieldnames=header):
        products[int(row["product_id"])].append(
            {"width": int(row["width"].strip()), "height": int(row["height"].strip())}
        )

# transform your grouped products into your desired result
result["sizes"] = [
    {"id": k, "product": {"product_creative_size_attributes": v}}
    for k, v in products.items()
]

output_json = dumps(result, indent=4)

print(output_json)

Output:

{
    "sizes": [
        {
            "id": 14866,
            "product": {
                "product_creative_size_attributes": [
                    {
                        "width": 200,
                        "height": 200
                    },
                    {
                        "width": 300,
                        "height": 300
                    }
                ]
            }
        }
    ]
}
import csv import json k_product_id = 0 k_width = 1 k_height = 2 json_dict = {} results = [] with open('sizes.csv', encoding='utf-8-sig') as f: reader = csv.reader(f) # Avoid header next(reader, None) for row in reader: # Find index of existing product id existing_id = list(i for i,v in enumerate(results) if v['id'] == int(row[k_product_id])) # If there is no existing product id if not existing_id: results.append(\ {\ "id": int(row[k_product_id]),\ "product":\ {\ "product_creative_size_attributes":\ [\ {\ "width": int(row[k_width]),\ "height": int(row[k_height])\ }\ ]\ }\ }\ ) # If there is existing product id else: results[existing_id[0]]['product']['product_creative_size_attributes']\ .append(\ {\ "width": int(row[k_width]),\ "height": int(row[k_height])\ }\ ) json_dict['sizes'] = results output_json = json.dumps(json_dict, indent=4)

暫無
暫無

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

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