CSV到JSON转换器(按相同键值分组)

y0u0uwnf  于 2023-04-22  发布在  其他
关注(0)|答案(2)|浏览(96)

我试图将csv格式转换为JSON,我谷歌我没有得到正确的方式来修改它,以获得所需的一个。
这是我的Python代码:

import csv
import json

def csv_to_json(csvFilePath, jsonFilePath):
    jsonArray = []

    #reading csv (encoding is important)
    with open(csvFilePath, encoding='utf-8') as csvf:
        #csv library function
        csvReader = csv.DictReader(csvf)

        #convert each csv row into python dictionary
        for column in csvReader:
            #add this python dictionary to json array
            jsonArray.append(column)

    #convertion
    with open(jsonFilePath, 'w', encoding='utf-8') as jsonf:
        jsonString = json.dumps(jsonArray, indent=4)
        jsonf.write(jsonString)

csvFilePath='example.csv'
jsonFilePath='output.json'
csv_to_json(csvFilePath, jsonFilePath)

这是我的csv文件格式:

我的实际JSON输出:

[
    {
        "Area": "IT",
        "Employee": "Carl",        
    },
    {
        "Area": "IT",
        "Employee": "Walter",      
    },
    {
        "Area": "Financial Resources",
        "Employee": "Jennifer",      
    }
]

我想要的JSON输出:

[
    {
        "Area": "IT",
        "Employee": ["Carl","Walter"],
    },
    {
      "Area": "Financial Resources",
      "Employee": ["Jennifer"],
    }
    
]

感谢您的评分

6jjcrrmo

6jjcrrmo1#

像这样的东西应该工作。

def csv_to_json(csvFilePath, jsonFilePath):
    areas = {}
    with open(csvFilePath, encoding='utf-8') as csvf:
        csvReader = csv.DictReader(csvf)
        for column in csvReader:
            area, employee = column["Area"], column["Employee"] # split values 
            if area in areas:  # add all keys and values to one dictionary
                areas[area].append(employee)
            else:
                areas[area] = [employee]
    # convert dictionary to desired output format.
    jsonArray = [{"Area": k, "Employee": v} for k,v in areas.items()]
    with open(jsonFilePath, 'w', encoding='utf-8') as jsonf:
        jsonString = json.dumps(jsonArray, indent=4)
        jsonf.write(jsonString)
a9wyjsp7

a9wyjsp72#

convtools库提供了很多reduce操作来处理聚合(* 我必须承认,我是作者 *):

from convtools import conversion as c
from convtools.contrib.tables import Table

# generates an ad-hoc function, which aggregates data
converter = (
    c.group_by(c.item("Area"))
    .aggregate(
        {
            "area": c.item("Area"),
            "employees": c.ReduceFuncs.Array(c.item("Employee")),
        }
    )
    .gen_converter()
)

result = converter(
    Table.from_csv("tmp/in.csv", header=True).into_iter_rows(dict)
)
assert result == [
    {"area": "IT", "employees": ["Carl", "Walter"]},
    {"area": "Financial Resources", "employees": ["Jennifer"]},
]

相关问题