Python 使用嵌套字典和格式进行显示
我这里有一个部分答案Python 使用嵌套字典和格式进行显示,python,dictionary,Python,Dictionary,我这里有一个部分答案 我的具体问题要求我从 这个 dir/file 10 dir/dir2/file2 20 dir/dir2/file3 10 dir/file3 10 dir3/file4 10 dir3/file5 10 到 最后的数字基本上是文件大小和 我一直在试图找出如何将所有文件的大小显示到父目录 编辑: r = re.compile(r'(.+\t)(\d+)') def prettify(d, indent=0): for k
我的具体问题要求我从 这个
dir/file 10
dir/dir2/file2 20
dir/dir2/file3 10
dir/file3 10
dir3/file4 10
dir3/file5 10
到
最后的数字基本上是文件大小和我一直在试图找出如何将所有文件的大小显示到父目录 编辑:
r = re.compile(r'(.+\t)(\d+)')
def prettify(d, indent=0):
for key, value in d.iteritems():
ss = 0
if key == FILE_MARKER:
if value:
for each in value:
mm = r.match(each)
ss += int(mm.group(2))
print ' ' * indent + each
***print ' ' * indent + format_size(ss)***
else:
print ' ' * indent + str(key)
if isinstance(value, dict):
addSizes(value, indent+1)
else:
print ' ' * (indent+1) + str(value)
这是mac从上面的链接中得到的答案,我编辑该链接以使用regExp我想到的解决方案使我创建了一个新的dict或添加了一个内部函数。
我失去了一整天的时间,真希望今天早些时候能得到帮助。
请提供帮助。当您处理输入时,为数字构建一个带有占位符(%d)的字符串,然后打印出字符串。这不是世界上最优雅的事情,但这应该能让您到达需要的位置。您需要更改树创建函数来处理您得到的任何形式的输入。一旦生成了树,它就只是使用递归树遍历来形成输出
import re
input_dirs = """dir/file 10
dir/dir2/file2 20
dir/dir2/file3 10
dir/file 10
dir3/file4 10
dir3/file5 10
dir/dir2/dir4/file2 10"""
def create_file_tree(input_string):
dir_dict = {}
for file_path in input_string.split('\n'):
path_list = re.sub('/',' ',file_path).split()
path_list[-1] = int(path_list[-1])
path_dict = dir_dict
final_item = ""
for item in path_list[:-1]:
parent_dict = path_dict
last_item = item
path_dict = path_dict.setdefault(item,{})
parent_dict[last_item] = path_list[-1]
return dir_dict
def pretty_file_tree(file_tree):
def traverse(sub_dict,indent=0, total=0):
string_out = ""
indent += 1
for key in sorted(sub_dict.keys()):
if type(sub_dict[key]) == dict:
sub_total = traverse(sub_dict[key],indent,0)
total += sub_total[0]
string_out += ' '*indent + key + ' ' + '**' + str(sub_total[0]) + '**' + '\n' + sub_total[1]
else:
string_out += ' '*indent + key + '\n'
total += sub_dict[key]
return total, string_out
output_string = traverse(file_tree)
print(output_string[1])
pretty_file_tree(create_file_tree(input_dirs))
抱歉,它没有遵循您发布的代码,但是我在编辑之前就已经开始制作了…@larsks我可以将大小相加,并在每个目录后的文件列表中显示出来,但仍然坚持在目录/行上显示。您应该向我们展示您尝试过的内容,然后我们就可以知道它是否优雅了。现在它可以像百万美元一样工作。非常感谢。我已经在一个单独的目录上测试了它我必须对分布在多个文件夹中的大量文本文件执行此操作。
import re
input_dirs = """dir/file 10
dir/dir2/file2 20
dir/dir2/file3 10
dir/file 10
dir3/file4 10
dir3/file5 10
dir/dir2/dir4/file2 10"""
def create_file_tree(input_string):
dir_dict = {}
for file_path in input_string.split('\n'):
path_list = re.sub('/',' ',file_path).split()
path_list[-1] = int(path_list[-1])
path_dict = dir_dict
final_item = ""
for item in path_list[:-1]:
parent_dict = path_dict
last_item = item
path_dict = path_dict.setdefault(item,{})
parent_dict[last_item] = path_list[-1]
return dir_dict
def pretty_file_tree(file_tree):
def traverse(sub_dict,indent=0, total=0):
string_out = ""
indent += 1
for key in sorted(sub_dict.keys()):
if type(sub_dict[key]) == dict:
sub_total = traverse(sub_dict[key],indent,0)
total += sub_total[0]
string_out += ' '*indent + key + ' ' + '**' + str(sub_total[0]) + '**' + '\n' + sub_total[1]
else:
string_out += ' '*indent + key + '\n'
total += sub_dict[key]
return total, string_out
output_string = traverse(file_tree)
print(output_string[1])
pretty_file_tree(create_file_tree(input_dirs))