从文件层次结构创建嵌套字典

时间:2016-01-28 19:57:18

标签: python file dictionary nested hierarchy

我想知道是否有人可以指出我正确的方向。我试图从文件路径列表创建一个嵌套字典,类似于下面。此列表将根据用户输入而更改,因此我认为它需要递归。关于从哪里开始的任何指示?

编辑:此外,字典将转换为JSON并用于使用D3.js创建图表。

fileDict = [
    {
        "name": "BaseLevel",
        "children": [
          {
            "name": "/etc/",
            "children": [
              {
                "name": "/etc/passwd",
              },
              {
                "name": "/etc/group"
              }
            ]
          },
          {
            "name": "/root/",
            "children": [
              {
                "name": "/root/test",
              }
            ]
          }
        ]
      }
    ]

我能够得到的最接近的例子是

    records = ["base/images/graphs/one.png", "base/images/tikz/two.png",
"base/refs/images/three.png", "base/one.txt", "base/chapters/two.txt"]

recordsSplit = map(lambda x: x.split("/"), records)

for record in recordsSplit:
    here = result
    for item in record[:-1]:
        if not item in here:
            here[item] = {}
            here = here[item]
        if "###content###" not in here:
            here["###content###"] = []
            here["###content###"].append(record[-1])

print json.dumps(result, indent=4)

2 个答案:

答案 0 :(得分:2)

是否值得上课而不是单词?写了一篇应该做你想做的短片

class FileSystem():

    def __init__(filePath=None):
        self.children = []
        if files != None:
            try:
                self.name, child = files.split("/", 2)
                self.children.append(FileSystem(filePath))
            except (ValueError):
                 pass

    def addChild(filePath):
        self.children.append(FileSystem(filePath))

    def getChildren():
        return self.children

    def printAllChildren():
        print "Name: "+ self.name
        print "{ Children:"
        for child in self.children:
            child.printAllChildren()
        print "}"

然后您可以输入第一个路径并保存对它的引用,如

myFileSystem = FileSystem("base/pictures/whatever.png")

这个myFileSystem将是您对“基础”级别的引用,并且使用它和它的方法,您应该能够做到你想要的。

然后,当您有第二条路径要添加时,您需要找到正确的节点,使用getChildren()上的myFileSystem添加它,直到找到差异,然后使用{{1}将文件路径的其余部分添加到该节点。 然后使用addChild()将打印出整个文件系统。

------- ------- EDIT

对我的半写代码不太满意并喜欢挑战所以这里是一个易于使用的课程

myFileSystem.printAllChildren()

正如你在最后我看到class FileSystem(): def __init__(self,filePath=None): self.children = [] if filePath != None: try: self.name, child = filePath.split("/", 1) self.children.append(FileSystem(child)) except (ValueError): self.name = filePath def addChild(self, filePath): try: thisLevel, nextLevel = filePath.split("/", 1) try: if thisLevel == self.name: thisLevel, nextLevel = nextLevel.split("/", 1) except (ValueError): self.children.append(FileSystem(nextLevel)) return for child in self.children: if thisLevel == child.name: child.addChild(nextLevel) return self.children.append(FileSystem(nextLevel)) except (ValueError): self.children.append(FileSystem(filePath)) def getChildren(self): return self.children def printAllChildren(self, depth = -1): depth += 1 print "\t"*depth + "Name: "+ self.name if len(self.children) > 0: print "\t"*depth +"{ Children:" for child in self.children: child.printAllChildren(depth) print "\t"*depth + "}" records = ["base/images/graphs/one.png", "base/images/tikz/two.png", "base/refs/images/three.png", "base/one.txt", "base/chapters/two.txt"] myFiles = FileSystem(records[0]) for record in records[1:]: myFiles.addChild(record) myFiles.printAllChildren() 时所看到的那样,addChild函数现在负责在树中找到合适的位置让它进入.printAllChildren()至少提供正确的输出对于那些参数。

让我知道它是否有任何意义,就像我说它没有经过充分测试所以一些极端情况(例如试图添加另一个基地?)可能会让它变得奇怪。

<强> EDIT2

myFiles.addChild(record)

答案 1 :(得分:0)

如果您有以下文件:

['testdata/hhohoho.mdf', 'testdata/dvojka/rerere.bdf', 'testdata/jedna/sss.txt']

你有输出结构,如:

Name: testdata
{ Children:
    Name: hhohoho.mdf
    Name: rerere.bdf
    Name: sss.txt
}

你错了:

self.children.append(FileSystem(nextLevel))
    except (ValueError):
        self.children.append(FileSystem(filePath))

解决了:

 self.children.append(FileSystem(thisLevel))
        for child in self.children:
            if thisLevel == child.name:
                child.addChild(nextLevel)
                return


Name: testdata
{ Children:
    Name: hhohoho.mdf
    Name: dvojka
    { Children:
            Name: rerere.bdf
    }
    Name: jedna
    { Children:
            Name: sss.txt
    }
}