目录树转 JSON

文件夹目录树转json集合

背景:为了处理2处不同来源的数据,就都转换成json集合,方便操作。

目录树结构

----demo\
    |----v1\
    |    |----init\
    |    |    |----time.py
    |    |----order.py
    |    |----surface\
    |    |    |----detail.py
    |    |    |----order.py
    |    |    |----user.py
    |    |----user.py
    |----v2\
    |    |----country.py
    |    |----list.py
    |----v3\
    |    |----languages.py
    |    |----surface\
    |    |    |----detail.py
    |    |    |----list.py
    |    |----user.py

以上是目录结构 首先进行读取,放到一个列表里,在读取的时候,还进行了正则匹配,匹配对应的py文件,以及文件内部的 方法。并且内部匹配到的每一条 方法生成一条记录

import sys
from pathlib import Path
import json
import re

tree_str = ''
tree_list = []

def generate_tree(pathname, n=0):
    global tree_str
    global tree_list
    if pathname.is_file():
        temp_file_name = pathname.name
        temp_file_name_list = temp_file_name.split('.')
        if len(temp_file_name_list) == 2 and temp_file_name_list[1] == 'py':
            file = open(pathname, 'r', encoding='utf-8')
            temp_action = []
            for line in file.readlines():
                line = line.strip()
                strName = getStrName(pathname.parent, temp_file_name_list[0])
                matchObj = re.match('def getAction()', line)
                if matchObj:
                    temp_action.append('get')

                matchObj = re.match('def postAction()', line)
                if matchObj:
                    temp_action.append('post')

                matchObj = re.match('def putAction()', line)
                if matchObj:
                    temp_action.append('put')

                matchObj = re.match('def deleteAction()', line)
                if matchObj:
                    temp_action.append('delete')
            if len(temp_action) > 0:
                temp_strName = strName + ',' + '_'.join(temp_action)
                tree_list.append(temp_strName.lower())
    elif pathname.is_dir():
        for cp in pathname.iterdir():
            generate_tree(cp, n + 1)

def getStrName(pathname, strName):
    if pathname.name != 'demo':
        strName = pathname.name + '/' + strName
        strName = getStrName(pathname.parent,strName)
    return strName

if __name__ == '__main__':
    p = Path(r'E:\www\myleetcode\demo')
    generate_tree(p)
    tree_list.sort()
    print(tree_list)

执行结果

['v1/init/time,get', 'v1/order,get', 'v1/surface/detail,post_get', 'v1/surface/order,post', 'v1/surface/user,post', 'v1/user,get', 'v2/country,put', 'v2/list,put', 'v3/languages,get', 'v3/surface/detail,delete', 'v3/surface/list,delete', 'v3/user,get']

以上生成了初步的记录,接下来就是转换成想要的json格式

转json

塞到字典里

    temp = {}
    for api in tree_list:
        temp_split = api.split('/')
        if len(temp_split) == 2:
            temp_str = temp_split[1].split(',')
            temp_api = {temp_str[0] : temp_str[1]}
            if temp_split[0] not in temp:
                temp[temp_split[0]] = temp_api
            else:
                temp[temp_split[0]].update(temp_api)
        elif len(temp_split) == 3:
            temp_str = temp_split[2].split(',')
            temp_api = {temp_str[0] : temp_str[1]}
            if  temp_split[0] not in temp:
                temp_surface = {temp_split[1] : temp_api}
                temp[temp_split[0]] = temp_surface
            else:
                temp_surface = temp[temp_split[0]]
                if temp_split[1] not in temp_surface:
                    temp_surface.update({temp_split[1]: temp_api})
                else:
                    temp_api_list = temp_surface[temp_split[1]]
                    temp_api_list.update(temp_api)
                    temp_surface[temp_split[1]] = temp_api_list
                temp[temp_split[0]] = temp_surface

    str_json = json.dumps(temp)
    print (str_json)

json结果

{"v1": {"init": {"time": "get"}, "order": "get", "surface": {"detail": "post_get", "order": "post", "user": "post"}, "user": "get"}, "v2": {"country": "put", "list": "put"}, "v3": {"languages": "get", "surface": {"detail": "delete", "list": "delete"}, "user": "get"}}

这样做的目的是方便下一步画出树,结构图能够清晰的比较差异。

本作品采用《CC 协议》,转载必须注明作者和本文链接
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!