将csv转换为JSON树结构并更改名称?

时间:2018-06-05 02:50:03

标签: python json csv tree

感谢Hatt的解释和代码。虽然我无法从列标题中更改有意义名称的字符串name,但它仍然有效。

任何人都可以建议如何实现这一目标吗?

csv文件中的数据

conversion_month    channel sub_channel campaign    Id  cost    kpi
2017-08 DISPLAY Retargeting Summer_Campaign 200278217   2.286261    0.1
2017-08 DISPLAY Retargeting Summer_Campaign 200278218   3.627064    2.5
2017-08 DISPLAY Retargeting Summer_Campaign 200278219   2.768436    0.001
2017-08 DISPLAY Retargeting August Campaign 200278220   5.653297    0.35
2017-09 DISPLAY Prospecting Test Campaign   200278221   4.11847 1.5
2017-08 DISPLAY Prospecting August Campaign 200278222   3.393972    0.26
2017-09 DISPLAY Prospecting Test Campaign   200278223   3.975332    4.2
2017-08 DISPLAY Prospecting August Campaign 200278224   4.131035    0.3

使用的代码:

import csv
from collections import defaultdict

def ctree():
    return defaultdict(ctree)

def build_leaf(name, leaf):
    res = {"name":name}
    # add children node if the leaf actually has any children
    if len(leaf.keys()) > 0:
        res["children"] = [build_leaf(k, v) for k, v in leaf.items()]
    return res

def main():
    tree = ctree()
    with open('file.csv') as csvfile:
        reader = csv.reader(csvfile)
        for rid, row in enumerate(reader):
            if rid == 0:       
                continue
            leaf = tree[row[0]]
            for cid in range(1, (len(row)-2)):
                leaf = leaf[row[cid]]
            for cid in range((len(row)-1), len(row)):
                leaf = (leaf[row[cid-1]],leaf[row[cid]])
    # building a custom tree structure
    res = []
    for name, leaf in tree.items():
        res.append(build_leaf(name, leaf))

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

main()

它给出了树,但我想更改字符串“name”以获取有意义的名称,例如“month”,“channel”,...“id”等。这些名称位于第一行csv文件。

[
  {
    "name": "2017-08",
    "children": [
      {
        "name": "DISPLAY",
        "children": [
          {
            "name": "Retargeting",
            "children": [
              {
                "name": "Summer_Campaign",
                "children": [
                  {
                    "name": "200278217",
                    "children": [
                      {
                        "name": "2.286261"
                      },
                      {
                        "name": "0.1"
                      }
                    ]

提前感谢您的任何建议。

1 个答案:

答案 0 :(得分:0)

使用SELECT TABLE_PROJECTEN.id, TABLE_PROJECTEN.code as pCode, TABLE_OPDRACHTEN.code as oCode, (SELECT COALESCE(SUM(t_kosten.aantal * t_kosten.bedrag),0) FROM `t_kosten` WHERE t_kosten.id_ref_project = TABLE_PROJECTEN.id) AS projectKosten, (SELECT COALESCE(SUM(t_opbrengsten.aantal * t_opbrengsten.bedrag),0) FROM `t_opbrengsten` WHERE t_opbrengsten.id_ref_project = TABLE_PROJECTEN.id) AS projectOpbrengsten, (SELECT COALESCE(SUM(t_kosten.aantal * t_kosten.bedrag),0) FROM `t_kosten` WHERE t_kosten.id_ref_opdracht =TABLE_OPDRACHTEN.id) AS opdrachtKosten (SELECT COALESCE(SUM(t_opbrengsten.aantal * t_opbrengsten.bedrag),0) FROM `t_opbrengsten` WHERE t_opbrengsten.id_ref_opdracht =TABLE_OPDRACHTEN.id) AS opdrachtOpbrengsten, (CALCULATE HERE: projectOpbrengsten-projectKosten) as projectTotal, (CALCULATE HERE: opdrachtOpbrengsten-opdrachtKosten) as opdrachtTotal FROM t_project TABLE_PROJECTEN LEFT JOIN t_opdracht TABLE_OPDRACHTEN on TABLE_OPDRACHTEN.id_ref_project = TABLE_PROJECTEN.id GROUP BY TABLE_OPDRACHTEN.code 首先从CSV文件中提取标题行。 next(reader)计数器可用于指示当前正在处理哪个列,以便可以从标题中提取相应的列标题:

level

这会给你:

import csv
from collections import defaultdict

def ctree():
    return defaultdict(ctree)

def build_leaf(name, leaf, level, header):
    res = {header[level] : name}
    # add children node if the leaf actually has any children
    if len(leaf.keys()) > 0:
        res["children"] = [build_leaf(k, v, level+1, header) for k, v in leaf.items()]
    return res

def main():
    tree = ctree()
    with open('file.csv') as csvfile:
        reader = csv.reader(csvfile)
        header = next(reader)

        for row in reader:
            leaf = tree[row[0]]
            for cid in range(1, (len(row)-2)):
                leaf = leaf[row[cid]]
            for cid in range((len(row)-1), len(row)):
                leaf = (leaf[row[cid-1]],leaf[row[cid]])

    # building a custom tree structure
    res = []
    for name, leaf in tree.items():
        res.append(build_leaf(name, leaf, 0, header))

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

main()