从python

时间:2016-03-14 09:59:41

标签: python json flask

表格:

categories = Table("categories", metadata,
                   Column("id", Integer, primary_key=True),
                   Column("name", String),
                   Column("parent_id", Integer, ForeignKey("categories.id"),
                          CheckConstraint('id!=parent_id'), nullable=True),

)

一个类别可以有很多孩子,但只有一个孩子。我使用CTE得到了如下字典值列表:例如。对于id:14,父亲是13并且从父8-> 10-> 12-> 13-> 14遍历,其中父8没有父id。

[
    {
      "id": 14, 
      "name": "cat14", 
      "parent_id": 13, 
      "path_info": [
        8, 
        10, 
        12, 
        13, 
        14
      ]
    }, 
    {
      "id": 15, 
      "name": "cat15", 
      "parent_id": 13, 
      "path_info": [
        8, 
        10, 
        12, 
        13, 
        15
      ]
    }
  ]

我想将父级的属性也作为子类别嵌入到列表中:

{
  "id": 14, 
  "name": "cat14", 
  "parent_id": 13, 
  "subcats": [
       {
         "id: 8", 
         "name": "cat8", 
         "parent_id":null
       }, 
       {
         "id: 10", 
         "name": "cat10", 
         "parent_id":8
       },  
       {
         "id: 12", 
         "name": "cat12", 
         "parent_id":10
       },   
      and similarly for ids 13 and 14..... 
     ]
}, 
{
  "id": 15, 
  "name": "cat15", 
  "parent_id": 13, 
  "subcats": [
       {
         "id: 8", 
         "name": "cat8", 
         "parent_id":null
       }, 
       {
         "id: 10", 
         "name": "cat10", 
         "parent_id":8
       },  
       {
         "id: 12", 
         "name": "cat12", 
         "parent_id":10
       },   
       and similarly for ids 13, 14, 15..... 
     ]
}

] 请注意,'path_info'已从字典中删除,并且每个id都已显示其详细信息。我想要以上面缩进格式的json转储。怎么去?使用烧瓶0.10,python 2.7

3 个答案:

答案 0 :(得分:2)

使用一些list / dict理解可以容忍这种方法。

lst = [{"id": 14, "name": "cat14", "parent_id": 13, "path_info": [8, 10, 12, 13, 14]}, {"id": 15, "name": "cat15", "parent_id": 13, "path_info": [8, 10, 12, 13, 15]}]

master_dct = { d['id'] : d for d in lst}
for d in lst:
    d['subcats'] = [{field : master_dct[i][field] for field in ['id', 'name', 'parent_id']} \
        for i in d['path_info'] if i in master_dct]

import json
with open('out.json', 'w') as f:
    json.dump(lst, f)

答案 1 :(得分:1)

您可以在python代码中执行它:

鉴于我们有一个json对象。我稍微修改了它 - 添加了缺少的节点并根据规范要求包装到对象中:

    {
      "array": [
          {
            "id": 14, 
            "name": "cat14", 
            "parent_id": 13, 
            "path_info": [
              8, 
              10, 
              12, 
              13, 
              14
            ]
          }, 
          {
            "id": 15, 
            "name": "cat15", 
            "parent_id": 13, 
            "path_info": [
              8, 
              10, 
              12, 
              13, 
              15
            ]
          },
          {
            "id": 13, 
            "name": "cat13", 
            "parent_id": 12, 
            "path_info": [
              8, 
              10, 
              12, 
              13
            ]
          },
         {
            "id": 12, 
            "name": "cat12", 
            "parent_id": 10, 
            "path_info": [
              8, 
              10, 
              12
            ]
          }, 
          {
            "id": 10, 
            "name": "cat10", 
            "parent_id": 8, 
            "path_info": [
              8, 
              10
            ]
          },
          {
            "id": 8, 
            "name": "cat8", 
            "parent_id": null, 
            "path_info": [
              8
            ]
          }
        ]
    }

然后您可以使用以下代码:

    # load data above from file
    j=json.load(open('json_file_above.json'))   # 

    #  the array with real data we need
    a=j['array']  

    # auxiliary dict which have node identificators as keys and nodes as values
    d={x['id']:x for x in a}  

    # here the magic begins :)
    for x in a:
       # add new key with list to each element
       x['subcats'] = [ 
                       # compose dict element for subcats
                       dict(id=i, name=d[i]['name'], parent_id=d[i]['parent_id'])
                       for 
                       i 
                       in [
                           # we take path_info id list and
                           # cut off the first element - itself
                           y for y in x['path_info'][1:]
                          ]  
                      ]
       del x['path_info']

确保你得到了你需要的东西:

    >>> print(json.dumps(a, indent=True))
    [
     {
      "name": "cat14",
      "subcats": [
       {
        "name": "cat10",
        "id": 10,
        "parent_id": 8
       },
       {
        "name": "cat12",
        "id": 12,
        "parent_id": 10
       },
       {
        "name": "cat13",
        "id": 13,
        "parent_id": 12
       },
       {
        "name": "cat14",
        "id": 14,
        "parent_id": 13
       }
      ],
      "id": 14,
      "parent_id": 13
     },
     {
      "name": "cat15",
      "subcats": [
       {
        "name": "cat10",
        "id": 10,
        "parent_id": 8
       },
       {
        "name": "cat12",
        "id": 12,
        "parent_id": 10
       },
       {
        "name": "cat13",
        "id": 13,
        "parent_id": 12
       },
       {
        "name": "cat15",
        "id": 15,
        "parent_id": 13
       }
      ],
      "id": 15,
      "parent_id": 13
     },
     {
      "name": "cat13",
      "subcats": [
       {
        "name": "cat10",
        "id": 10,
        "parent_id": 8
       },
       {
        "name": "cat12",
        "id": 12,
        "parent_id": 10
       },
       {
        "name": "cat13",
        "id": 13,
        "parent_id": 12
       }
      ],
      "id": 13,
      "parent_id": 12
     },
     {
      "name": "cat12",
      "subcats": [
       {
        "name": "cat10",
        "id": 10,
        "parent_id": 8
       },
       {
        "name": "cat12",
        "id": 12,
        "parent_id": 10
       }
      ],
      "id": 12,
      "parent_id": 10
     },
     {
      "name": "cat10",
      "subcats": [
       {
        "name": "cat10",
        "id": 10,
        "parent_id": 8
       }
      ],
      "id": 10,
      "parent_id": 8
     },
     {
      "name": "cat8",
      "subcats": [],
      "id": 8,
      "parent_id": null
     }
    ]
    >>>

答案 2 :(得分:1)

这个pythonic代码:简单明了

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]  

<Files "index.php">
 AcceptPathInfo On
</Files>