【问题标题】:Working with nested dictionaries and formatting for display使用嵌套字典和格式化显示
【发布时间】:2013-08-07 14:32:54
【问题描述】:

我从这里得到了部分答案Construct a tree from list os file paths (Python) - Performance dependent

我的具体问题需要我从

这个

dir/file  10  
dir/dir2/file2  20  
dir/dir2/file3  10
dir/file3  10  
dir3/file4  10  
dir3/file5  10

dir/  **50**     
    dir2/  **30**    
        file2  
        file3
    file
    file3  
dir3/  **20**  
    file4  
    file5  

基本上最后的数字是文件大小和
我一直在试图弄清楚如何将所有文件的大小显示到父目录

编辑:

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)  

这是我使用 regExp 编辑的上述链接中的 mac 答案
我想到的解决方案让我创建了一个新的 dict 或添加了一个内部函数。
我已经失去了一整天,希望我能在当天早些时候寻求帮助。
请帮忙。

【问题讨论】:

  • @larsks 我可以将大小相加并将其显示在每个目录之后的文件列表之后,但坚持在 dir/ 行上显示它。
  • 您应该向我们展示您的尝试,然后我们可以提供帮助

标签: python dictionary


【解决方案1】:

这不是世界上最优雅的东西,但这应该能让你到达你需要的地方。您需要更改树创建功能以处理您获得的任何形式的输入。生成树后,它只是使用递归树遍历来形成输出。

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))

对不起,它没有遵循您发布的代码,但我在编辑之前就开始制作了...

【讨论】:

  • 优雅与否它现在像一百万美元一样工作。所以非常感谢。我现在已经在一个文件上对其进行了测试。我必须针对分布在其中的大量文本文件运行它多个文件夹。
【解决方案2】:

在处理输入时,为数字构建一个带有占位符 (%d) 的字符串,然后打印出该字符串。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-07
    • 2013-11-24
    • 1970-01-01
    相关资源
    最近更新 更多