Python 将csv文件映射到嵌套json

Python 将csv文件映射到嵌套json,python,json,csv,dictionary,Python,Json,Csv,Dictionary,我有csv格式的数据 "category1", 2010, "deatil1" "category1", 2010, "deatil2" "category1", 2011, "deatil3" "category2", 2011, "deatil4" 我需要以 { "name": "Data", "children": [{ "name": "category1", "children": [{

我有csv格式的数据

"category1", 2010, "deatil1"
"category1", 2010, "deatil2"
"category1", 2011, "deatil3"
"category2", 2011, "deatil4"
我需要以

 {
        "name": "Data",
        "children": [{
            "name": "category1",
            "children": [{
                "name": "2010",
                "children": [
                    {"name": "deatil1"}, 
                    {"name": "detail2"}
                 ],
                "name": "2011",
                "children": [
                    {"name": "detail3"}
                ]
            }, {
            }]
        }, 
            {
            "name": "category2",
            "children": [{
                    "name": "2011",
                    "children": [{
                        "name": "detail4"
                    }]
                }
            ]
        }
    ]
    }
基本上,我需要收集每个独特类别和年份对的所有细节,并列出清单

我尝试使用嵌套的dict结构,但输出不正确

我创建了一个自定义dict类,用于处理字典嵌套的嵌套。下面的代码以正确的结构收集数据,但我不确定如何继续以正确的格式输出数据。任何帮助都将不胜感激

class Vividict(dict):

    def __missing__(self, key):
        value = self[key] = type(self)()
        return value

dict = Vividict()

for row in ws.iter_rows(row_offset=1):
    sector = row[0].value
    year =  row[2].value
    detail = row[1].value
    dict[sector][year][detail]

print json.dumps(dict).encode('utf8')

从您的
dict
结构开始,就是建立新的数据结构。我在这里使用的主要是创建
dict
的列表理解:

import json

rows = [
    ("category1", 2010, "deatil1"),
    ("category1", 2010, "deatil2"),
    ("category1", 2011, "deatil3"),
    ("category2", 2011, "deatil4")]

class Vividict(dict):
    def __missing__(self, key):
        value = self[key] = type(self)()
        return value

dict = Vividict()

for row in rows:
    sector = row[0]
    year = row[1]
    detail = row[2]
    dict[sector][year][detail]

# This is the new data structure, derived from the existing 'dict'
d = {'name': 'Data',
     'children': [
         {'name': k1,  # sector
          'children': [
                       {'name': k2,  # year
                        'children': [
                            {
                             'name': k3  # deatil
                             } for k3 in v2.keys()]
                        } for k2, v2 in v1.iteritems()]
          } for k1, v1 in dict.iteritems()]
     }

print json.dumps(d).encode('utf8')

在这里查看它的实际操作:

从您的
dict
结构开始,建立新的数据结构是一个问题。我在这里使用的主要是创建
dict
的列表理解:

import json

rows = [
    ("category1", 2010, "deatil1"),
    ("category1", 2010, "deatil2"),
    ("category1", 2011, "deatil3"),
    ("category2", 2011, "deatil4")]

class Vividict(dict):
    def __missing__(self, key):
        value = self[key] = type(self)()
        return value

dict = Vividict()

for row in rows:
    sector = row[0]
    year = row[1]
    detail = row[2]
    dict[sector][year][detail]

# This is the new data structure, derived from the existing 'dict'
d = {'name': 'Data',
     'children': [
         {'name': k1,  # sector
          'children': [
                       {'name': k2,  # year
                        'children': [
                            {
                             'name': k3  # deatil
                             } for k3 in v2.keys()]
                        } for k2, v2 in v1.iteritems()]
          } for k1, v1 in dict.iteritems()]
     }

print json.dumps(d).encode('utf8')

请在此处查看操作:

实际上您正在使用字典理解!事实上,你正在使用字典理解!