Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 加上“;“尺寸”;元素到sunburst图的最后一个json子元素_Python_Json_Csv_D3.js_Sunburst Diagram - Fatal编程技术网

Python 加上“;“尺寸”;元素到sunburst图的最后一个json子元素

Python 加上“;“尺寸”;元素到sunburst图的最后一个json子元素,python,json,csv,d3.js,sunburst-diagram,Python,Json,Csv,D3.js,Sunburst Diagram,我有一个工作的python脚本,它获取我的csv列数据,并将其转换为json文件,供我的d3 sunburst可视化读取。问题是在最后一个子元素中没有正确填充sunburst图表所需的“size”元素 下面是我的脚本,它以我需要的方式将csv读取为json。我尝试使用if-else循环修改脚本,以找到没有子元素(最后一个元素)的地方,然后在该元素上附加“size:1”,但什么也没有发生 这是csv数据的示例。不过,代码应该适用于任何情况 能量、修饰、脱落、可训练性、群体、品种 定期运动,每周2-

我有一个工作的python脚本,它获取我的csv列数据,并将其转换为json文件,供我的d3 sunburst可视化读取。问题是在最后一个子元素中没有正确填充sunburst图表所需的“size”元素

下面是我的脚本,它以我需要的方式将csv读取为json。我尝试使用if-else循环修改脚本,以找到没有子元素(最后一个元素)的地方,然后在该元素上附加“size:1”,但什么也没有发生

这是csv数据的示例。不过,代码应该适用于任何情况

能量、修饰、脱落、可训练性、群体、品种

定期运动,每周2-3次刷牙,季节性,轻松训练,玩具组,艾芬宾谢

最后一个子元素需要如下所示:

[
{
    "name": "Regular Exercise",
    "children": [
        {
            "name": "2-3 Times a Week Brushing",
            "children": [
                {
                    "name": "Seasonal",
                    "children": [
                        {
                            "name": "Easy Training",
                            "children": [
                                {
                                    "name": "Toy Group",
                                    "children": [
                                        {
                                            "name": "Affenpinscher",
                                            "size": 1
                                        }
                                    ]
                                }]}]}]}]}]}

要将
size
添加到最后一个条目:

import csv
from collections import defaultdict
import json
#import uuid
#from IPython.display import display_javascript, display_html, display


def ctree():
    return defaultdict(ctree)


def build_leaf(name, leaf):
    res = {"name": name}

    # add children node if the leaf actually has any children
    if leaf.keys():
        res["children"] = [build_leaf(k, v) for k, v in leaf.items()]
    else:
        res['size'] = 1

    return res


def main():
    tree = ctree()
    # NOTE: you need to have test.csv file as neighbor to this file
    with open('test.csv') as csvfile:
        reader = csv.reader(csvfile)
        header = next(reader)  # read the header row

        for row in reader:
            # usage of python magic to construct dynamic tree structure and
            # basically grouping csv values under their parents
            leaf = tree[row[0]]

            for value in row[1:]:
                leaf = leaf[value]

    # building a custom tree structure
    res = []

    for name, leaf in tree.items():
        res.append(build_leaf(name, leaf))

    # printing results into the terminal
    print(json.dumps(res, indent=2))


main()

要将
size
添加到最后一个条目:

import csv
from collections import defaultdict
import json
#import uuid
#from IPython.display import display_javascript, display_html, display


def ctree():
    return defaultdict(ctree)


def build_leaf(name, leaf):
    res = {"name": name}

    # add children node if the leaf actually has any children
    if leaf.keys():
        res["children"] = [build_leaf(k, v) for k, v in leaf.items()]
    else:
        res['size'] = 1

    return res


def main():
    tree = ctree()
    # NOTE: you need to have test.csv file as neighbor to this file
    with open('test.csv') as csvfile:
        reader = csv.reader(csvfile)
        header = next(reader)  # read the header row

        for row in reader:
            # usage of python magic to construct dynamic tree structure and
            # basically grouping csv values under their parents
            leaf = tree[row[0]]

            for value in row[1:]:
                leaf = leaf[value]

    # building a custom tree structure
    res = []

    for name, leaf in tree.items():
        res.append(build_leaf(name, leaf))

    # printing results into the terminal
    print(json.dumps(res, indent=2))


main()

这实际上将size元素应用于所有name元素。我只需要在没有子元素的lastname元素/name元素上添加一个size元素。这就足够了吗?你能添加一个CSV的小样本来重新创建问题吗?我明天要看一看,你尝试过更新的脚本了吗?我尝试过了,但是脚本不喜欢如果last:res['size']=1,然后build_leaf(k,v)说它缺少last的值。。。仍在试验这实际上将size元素应用于所有name元素。我只需要在没有子元素的lastname元素/name元素上添加一个size元素。这就足够了吗?你能添加一个CSV的小样本来重新创建问题吗?我明天要看一看,你尝试过更新的脚本了吗?我尝试过了,但是脚本不喜欢如果last:res['size']=1,然后build_leaf(k,v)说它缺少last的值。。。还在试验