如何将目录结构解析为字典?

use*_*345 6 python

我有目录结构列表,例如:

['/a/b', '/a/b/c', '/a/b/c/d', '/a/b/c/e', '/a/b/c/f/g', '/a/b/c/f/h', '/a/b/c/f/i']
Run Code Online (Sandbox Code Playgroud)

我想把它像树结构一样转换成字典。

{'/': {'a': {'b': {'c': 
                       [{'d':None}, 
                        {'e':None}, 
                        {'f':[{'g':None, {'h':None}, {'i':None}]}
                       ]
                  }
             }
      }
}
Run Code Online (Sandbox Code Playgroud)

我被困在哪里战略?哪种数据结构适合?

谢谢。

geo*_*org 7

基本上

lst = ['/a/b', '/a/b/c', '/a/b/c/d', '/a/b/c/e', '/a/b/c/f/g', '/a/b/c/f/h', '/a/b/c/f/i']
dct = {}

for item in lst:
    p = dct
    for x in item.split('/'):
        p = p.setdefault(x, {})

print dct
Run Code Online (Sandbox Code Playgroud)

产生

 {'': {'a': {'b': {'c': {'e': {}, 'd': {}, 'f': {'i': {}, 'h': {}, 'g': {}}}}}}}
Run Code Online (Sandbox Code Playgroud)

这不完全是你的结构,但应该给你一个基本的想法。


Tho*_*ent 6

正如Sven Marnach 所说,输出数据结构应该更加一致,例如只有嵌套字典,其中文件夹与 dict 相关联,文件与 None 相关联。

这是一个使用os.walk的脚本。它不接受列表作为输入,但如果您想解析文件,最终应该做您想做的事情。

import os 
from pprint import pprint

def set_leaf(tree, branches, leaf):
    """ Set a terminal element to *leaf* within nested dictionaries.              
    *branches* defines the path through dictionnaries.                            

    Example:                                                                      
    >>> t = {}                                                                    
    >>> set_leaf(t, ['b1','b2','b3'], 'new_leaf')                                 
    >>> print t                                                                   
    {'b1': {'b2': {'b3': 'new_leaf'}}}                                             
    """
    if len(branches) == 1:
        tree[branches[0]] = leaf
        return
    if not tree.has_key(branches[0]):
        tree[branches[0]] = {}
    set_leaf(tree[branches[0]], branches[1:], leaf)

startpath = '.'
tree = {}
for root, dirs, files in os.walk(startpath):
    branches = [startpath]
    if root != startpath:
        branches.extend(os.path.relpath(root, startpath).split('/'))

    set_leaf(tree, branches, dict([(d,{}) for d in dirs]+ \
                                  [(f,None) for f in files]))

print 'tree:'
pprint(tree)
Run Code Online (Sandbox Code Playgroud)