Python 从文件层次结构创建嵌套字典
我想知道是否有人能给我指出正确的方向。我试图从文件路径列表中创建一个嵌套字典,类似于下面的内容。这个列表将根据用户的输入而改变,所以我认为它需要是递归的。有关于从哪里开始的建议吗 编辑:此外,字典将转换为JSON,并用于使用D3.js创建图形Python 从文件层次结构创建嵌套字典,python,file,dictionary,nested,hierarchy,Python,File,Dictionary,Nested,Hierarchy,我想知道是否有人能给我指出正确的方向。我试图从文件路径列表中创建一个嵌套字典,类似于下面的内容。这个列表将根据用户的输入而改变,所以我认为它需要是递归的。有关于从哪里开始的建议吗 编辑:此外,字典将转换为JSON,并用于使用D3.js创建图形 fileDict = [ { "name": "BaseLevel", "children": [ { "name": "/etc/", "chil
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)
它值得上一堂课而不是听写吗?写了一篇短文,你想怎么做就怎么做
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
将是您对“基本”级别的引用,使用它和它的方法,您应该能够做您想要做的事情
然后,当您有第二个要添加的路径时,您必须使用myFileSystem
上的getChildren()
找到要添加到的正确节点,直到发现不一致,然后使用addChild()
将文件路径的其余部分添加到该节点。
然后使用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()
正如您在最后看到的,当我简单地执行myFiles.addChild(record)
时,addChild函数现在负责在树中找到它要进入的正确位置。printAllChildren()至少为这些参数提供正确的输出
如果其中任何一个没有意义,请告诉我,就像我说的,它没有经过充分测试,因此一些角落案例(例如,试图添加另一个基地?)可能会让它变得怪异
EDIT2
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 + "}"
def makeDict(self):
if len(self.children) > 0:
dictionary = {self.name:[]}
for child in self.children:
dictionary[self.name].append(child.makeDict())
return dictionary
else:
return self.name
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)
print myFiles.makeDict()
当您有以下文件时:
['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
}
}
这个答案太棒了,非常有用,谢谢。不幸的是,我没有提到的错误是,它必须是一个字典,因为它需要转换为JSON,并使用D3.jso显示。哦,我明白了,对不起,我应该阅读您的尝试,您在那里有一个
JSON
关键字。不幸的是,我对JSON知之甚少,但是您肯定可以将我的解决方案塑造成一个实际的dict。一种方法是扩展dict类(字面上将该类声明为类文件系统(dict)):
然后编辑当前方法,以便将self.name
设置为键,将self.children
设置为值。或者使用makeIntoDict()
方法生成并返回dict(与printAll()当前的方式类似),通过设置键&self.name
作为值&self.children
。如果没有其他人提出任何建议,我可以稍后再尝试一下。请参见编辑2,虽然有点匆忙,但我认为它是有效的。很抱歉,回复太晚了,我周末不在。您的第二次编辑工作得很好,谢谢!您的示例仅显示了一个single级别。您是想只按目录的第一级对路径进行分类,还是想建立一个嵌套字典的“树”,为路径的每一级建立一个单独的字典?想建立一个嵌套字典的树。我的目标是最终得到一个d3.js图,树结构表示文件层次结构