gpt4 book ai didi

python - 使用没有值的列从 csv 创建嵌套 JSON 的问题

转载 作者:行者123 更新时间:2023-12-03 14:38:06 25 4
gpt4 key购买 nike

提前感谢您的帮助。
我有一个具有以下结构的 csv 文件:

group1,group2,group3,name,info
General,Nation,,Phil,info1
General,Nation,,Karen,info2
General,Municipality,,Bill,info3
General,Municipality,,Paul,info4
Specific,Province,,Patrick,info5
Specific,Province,,Maikel,info6
Specific,Province,Governance,Mike,info7
Specific,Province,Governance,Luke,info8
Specific,District,,Maria,info9
Specific,District,,David,info10

我需要一个嵌套的 JSON 用于 D3 或 amcharts。
使用此页面上的 python 脚本( https://github.com/hettmett/csv_to_json ),我可以创建一个嵌套的 JSON。

结果如下所示:
[
{
"name" : "General",
"children" : [
{
"name" : "Nation",
"children" : [
{
"name" : "",
"children" : [
{
"name" : "Phil",
"children" : [
{
"name" : "info1"
}
]
},
{
"name" : "Karen",
"children" : [
{
"name" : "info2"
}
]
}
]
}
]
},
{
"name" : "Municipality",
"children" : [
{
"name" : "",
"children" : [
{
"name" : "Bill",
"children" : [
{
"name" : "info3"
}
]
},
{
"name" : "Paul",
"children" : [
{
"name" : "info4"
}
]
}
]
}
]
}
]
},
{
"name" : "Specific",
"children" : [
{
"name" : "Province",
"children" : [
{
"name" : "",
"children" : [
{
"name" : "Patrick",
"children" : [
{
"name" : "info5"
}
]
},
{
"name" : "Maikel",
"children" : [
{
"name" : "info6"
}
]
}
]
},
{
"name" : "Governance",
"children" : [
{
"name" : "Mike",
"children" : [
{
"name" : "info7"
}
]
},
{
"name" : "Luke",
"children" : [
{
"name" : "info8"
}
]
}
]
}
]
},
{
"name" : "District",
"children" : [
{
"name" : "",
"children" : [
{
"name" : "Maria",
"children" : [
{
"name" : "info9"
}
]
},
{
"name" : "David",
"children" : [
{
"name" : "info10"
}
]
}
]
}
]
}
]
}
]

然而,这并不完全是我所需要的。问题是某些列没有值,因此不应在去嵌套的 JSON 中包含子项。
像这样:
"name": "Overview",
"children": [{
"name": "General",
"children": [
{ "name": "Nation",
"children": [
{"name": "Phil", "info": "info1"},
{"name": "Karen", "info": "info2"}
]
},
{ "name": "Municipality",
"children": [
{"name": "Bill", "info": "info3"},
{"name": "Paul", "info": "info4"}
]
}
]


},
{
"name": "Specific",
"children": [
{ "name": "Province",
"children": [
{"name": "Patrick", "info": "info5"},
{"name": "Maikel", "info": "info6"},
{"name": "Governance",
"children": [
{"name": "Mike", "info": "info7"},
{"name": "Luke", "info": "info8"}
]
}
]
},
{ "name": "District",
"children": [
{"name": "Maria", "info": "info9"},
{"name": "David", "info": "info10"}
]
}
]
}
]

希望有人能帮忙。

亲切的问候
斯特凡

最佳答案

您的“理想”结果与脚本给出的结果之间实际上存在两个有意义的差异:

  • 删除空字符串(如您所见)。
  • 最后一个 child 的格式为:{"name": "xxxxx", "info": "yyyyy"}而不是 {"name": "xxxxx", "children": [{"name": "yyyyy"}]} .

  • 所以我们可以解决这两个问题:

    假设您已经定义了 js_objs作为 csv-to-json 的结果你上面提到的图书馆。

    from copy import deepcopy


    def remove_empties(children):
    """Just removes the empty name string levels."""
    for i, js in enumerate(children):
    if js['name'] == '':
    children.pop(i)
    if 'children' in js:
    for child_js in js['children'][::-1]:
    children.insert(i, child_js)
    if i < len(children):
    js = children[i]
    else:
    raise StopIteration('popped off a cap')
    for i, js in enumerate(children):
    if 'children' in js:
    js['children'] = remove_empties(js['children'])
    return children


    def parse_last_child(js):
    """Looks for the last child and formats that one correctly"""
    if 'children' not in js:
    print(js)
    raise ValueError('malformed js')
    if len(js['children']) == 1 and 'children' not in js['children'][0]:
    js['info'] = js.pop('children')[0]['name']
    else:
    js['children'] = [parse_last_child(j) for j in js['children']]
    return js


    accumulator = deepcopy(js_objs) # so we can compare against the original
    non_empties = remove_empties(accumulator)
    results = [parse_last_child(x) for x in non_empties]

    我得到的结果是......

    [{'name': 'General',
    'children': [{'name': 'Nation',
    'children': [{'name': 'Phil', 'info': 'info1'},
    {'name': 'Karen', 'info': 'info2'}]},
    {'name': 'Municipality',
    'children': [{'name': 'Bill', 'info': 'info3'},
    {'name': 'Paul', 'info': 'info4'}]}]},
    {'name': 'Specific',
    'children': [{'name': 'Province',
    'children': [{'name': 'Patrick', 'info': 'info5'},
    {'name': 'Maikel', 'info': 'info6'},
    {'name': 'Governance',
    'children': [{'name': 'Mike', 'info': 'info7'},
    {'name': 'Luke', 'info': 'info8'}]}]},
    {'name': 'District',
    'children': [{'name': 'Maria', 'info': 'info9'},
    {'name': 'David', 'info': 'info10'}]}]}]

    笔记:
    只要您的 json 对象不太深,这就会起作用。否则你会达到递归深度。

    只是为了澄清,在这种情况下:

    js_objs = [
    {
    "name" : "General",
    "children" : [
    {
    "name" : "Nation",
    "children" : [
    {
    "name" : "",
    "children" : [
    {
    "name" : "Phil",
    "children" : [
    {
    "name" : "info1"
    }
    ]
    },
    {
    "name" : "Karen",
    "children" : [
    {
    "name" : "info2"
    }
    ]
    }
    ]
    }
    ]
    },
    {
    "name" : "Municipality",
    "children" : [
    {
    "name" : "",
    "children" : [
    {
    "name" : "Bill",
    "children" : [
    {
    "name" : "info3"
    }
    ]
    },
    {
    "name" : "Paul",
    "children" : [
    {
    "name" : "info4"
    }
    ]
    }
    ]
    }
    ]
    }
    ]
    },
    {
    "name" : "Specific",
    "children" : [
    {
    "name" : "Province",
    "children" : [
    {
    "name" : "",
    "children" : [
    {
    "name" : "Patrick",
    "children" : [
    {
    "name" : "info5"
    }
    ]
    },
    {
    "name" : "Maikel",
    "children" : [
    {
    "name" : "info6"
    }
    ]
    }
    ]
    },
    {
    "name" : "Governance",
    "children" : [
    {
    "name" : "Mike",
    "children" : [
    {
    "name" : "info7"
    }
    ]
    },
    {
    "name" : "Luke",
    "children" : [
    {
    "name" : "info8"
    }
    ]
    }
    ]
    }
    ]
    },
    {
    "name" : "District",
    "children" : [
    {
    "name" : "",
    "children" : [
    {
    "name" : "Maria",
    "children" : [
    {
    "name" : "info9"
    }
    ]
    },
    {
    "name" : "David",
    "children" : [
    {
    "name" : "info10"
    }
    ]
    }
    ]
    }
    ]
    }
    ]
    }
    ]

    关于python - 使用没有值的列从 csv 创建嵌套 JSON 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61336591/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com