首页 > 解决方案 > 将 Flatten CSV 转换为嵌套 JSON

问题描述

我想从展平 CSV 文件中嵌套 JSON,如何使用展平 CSV 文件中的所有子类别创建父类别。请查看下面给定图像中的数据格式。
格式的图像在这里

任何帮助,将不胜感激!

标签: pythonpandas

解决方案


You can read about csv module here: https://docs.python.org/3/library/csv.html

There is a class there called DictReader which takes a CSV file and puts it in a dictionary, and from there the way to json is easy.

The following example is presented there:

import csv
with open('names.csv', newline='') as csvfile:
    reader = csv.DictReader(csvfile)
    for row in reader:
        print(row['first_name'], row['last_name'])

Output:

Eric Idle John Cleese


推荐阅读