【问题标题】:Recursively print a tree structure of dictionaries递归打印字典的树结构
【发布时间】:2020-07-14 21:45:02
【问题描述】:

所以我正在寻求一些帮助来编写如下图所示的函数。我将发布所提供函数的代码,并发布我目前所做的工作。

Assignment description part 1

Assignment Desciption part 2

提供的功能:

def print_file_listing(file_listing, indent=""):
"""Format and print file_listing, a dictionary representing the
description of a disk file.

Parameters:

    file_listing:  a dictionary with keys "name", "timestamp",
        "type" and "size".  The "type" value (always "file") is
        omitted from the printed output.

    indent (optional):  a string of spaces representing the
        level at which the file is nested in the folder
        structure.
"""
print(f"{indent}{file_listing['name']:<12} "\
      f"{file_listing['timestamp']} "\
      f"{file_listing['size']:>8}")

到目前为止我的代码:

def print_folder(folder, indent="0"):
"""Format and print folder, a dictionary representing a file
system.

Parameters:

    folder:  a dictionary with keys "name", "timestamp", "type" and
        "files".  "files" is a list of zero or more files and
        folders.  The "type" value (always "dir") is omitted from
        the printed output.

    indent (optional):  a string of spaces representing the
        level at which the folder is nested in the file system.
"""
print(f"{indent}{folder['name']:<12} {folder['timestamp']}")
if(folder['type']=='dir'):
    for checksubDir in (folder['files']):#handles sub directories
        print_folder(checksubDir, indent+1)
if(folder['type']=='file'):
    for s in range(0,indent):#looping for spaces
        print(' ')
        print_file_listing(folder)

`def main(): root = json_reader.read_json('dir_tree_1.json') 打印文件夹(根)

主() `

【问题讨论】:

  • pprint.pprint 听起来像您要找的东西。
  • 实际问题是什么? “我正在寻求家庭作业的帮助”不是问题......

标签: python python-3.x dictionary recursion tree


【解决方案1】:

TypeError: 只能将 str(不是“int”)连接到 str

【讨论】:

  • 如果您想澄清问题,请使用 cmets。
猜你喜欢
  • 2022-09-27
  • 1970-01-01
  • 1970-01-01
  • 2012-10-22
  • 2014-06-28
  • 2020-03-17
  • 1970-01-01
  • 1970-01-01
  • 2015-02-14
相关资源
最近更新 更多