简体   繁体   English

使用 python 打印 JSON 文件时,我的递归函数无法正确放置选项卡。 我该如何解决?

[英]My recursive function won't properly put tabs when printing out a JSON file when using python. How can I fix it?

My function我的功能

def string_info(json_info, string):
    if type(json_info) == dict:
        for key in json_info.keys():
            other = string_info(json_info[key], "")
            string += f"\n{key}:\t{other}"
    elif type(json_info) == list:
        for index in range(0, len(json_info)):
            string += f"{json_info[index]} "
    else:
        string += f"{json_info}"
    return string

Sample JSON示例 JSON

"statistics": {
        "level": {
            "current": 100,
            "progress": 52
        },
        "pp": 5934,
        "pp_rank": 15496,
        "ranked_score": 15283968302,
        "hit_accuracy": 98.3355,
        "play_count": 76169,
        "play_time": 3855235,
        "total_score": 79160699555,
        "total_hits": 13126452,
        "maximum_combo": 2104,
        "replays_watched_by_others": 24,
        "is_ranked": true,
        "grade_counts": {
            "ss": 51,
            "ssh": 22,
            "s": 1202,
            "sh": 224,
            "a": 1272
        },
        "rank": {
            "global": 15496,
            "country": 2553
        }
    }

My Output我的输出


level:  
current:    100
progress:   52
pp: 5934
pp_rank:    15496
ranked_score:   15283968302
hit_accuracy:   98.3355
play_count: 76169
play_time:  3855235
total_score:    79160699555
total_hits: 13126452
maximum_combo:  2104
replays_watched_by_others:  24
is_ranked:  True
grade_counts:   
ss: 51
ssh:    22
s:  1202
sh: 224
a:  1272
rank:   
global: 15496
country:    2553

It seems like the tabs are not the same size for every line and I was expecting an output more along the lines of似乎每一行的选项卡大小都不相同,我期望输出更多


level:  
   current:   100
   progress:   52
pp:   5934
pp_rank:   15496
ranked_score:   15283968302
hit_accuracy:   98.3355
play_count:   76169
play_time:   3855235
total_score:   79160699555
total_hits:   13126452
maximum_combo:   2104
replays_watched_by_others:   24
is_ranked:   True
grade_counts:   
   ss:   51
   ssh:   22
   s:   1202
   sh:   224
   a:   1272
rank:   
   global:   15496
   country:   2553

I'm pretty new to programming with python, and I was wondering what mistake I have made to have inconsistent tabs and having the spacing not as I expected in the output.我对使用 python 编程还很陌生,我想知道我犯了什么错误,导致制表符不一致,并且输出中的间距不符合我的预期。 Thanks for any help.谢谢你的帮助。

(Also, the if statement for the if is for other types of data I might be getting from a JSON) (此外,if 的 if 语句适用于我可能从 JSON 中获取的其他类型的数据)

# fixed tab = 3 spaces
mytab = ' '*3

def string_info(json_info):
    string = ""
    if type(json_info) == dict:
        for key, value in json_info.items():
            other = string_info(value)
            other_tabbed = f'\n{mytab}'.join(other.split('\n'))
            string += f"\n{key}:{mytab}{other_tabbed}"
    elif type(json_info) == list:
        for index in range(0, len(json_info)):
            string += f"{json_info[index]} "
    else:
        string += f"{json_info}"
    return string


# test
import json

s = '''
{
"statistics": {
        "level": {
            "current": 100,
            "progress": 52
        },
        "pp": 5934,
        "pp_rank": 15496,
        "ranked_score": 15283968302,
        "hit_accuracy": 98.3355,
        "play_count": 76169,
        "play_time": 3855235,
        "total_score": 79160699555,
        "total_hits": 13126452,
        "maximum_combo": 2104,
        "replays_watched_by_others": 24,
        "is_ranked": true,
        "grade_counts": {
            "ss": 51,
            "ssh": 22,
            "s": 1202,
            "sh": 224,
            "a": 1272
        },
        "rank": {
            "global": 15496,
            "country": 2553
        }
    }
 }
'''

data = json.loads(s)
print(string_info(data))

Output:输出:

statistics:   
   level:   
      current:   100
      progress:   52
   pp:   5934
   pp_rank:   15496
   ranked_score:   15283968302
   hit_accuracy:   98.3355
   play_count:   76169
   play_time:   3855235
   total_score:   79160699555
   total_hits:   13126452
   maximum_combo:   2104
   replays_watched_by_others:   24
   is_ranked:   True
   grade_counts:   
      ss:   51
      ssh:   22
      s:   1202
      sh:   224
      a:   1272
   rank:   
      global:   15496
      country:   2553

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 打印循环时如何执行0? 我一直在尝试在 python 中打印出阶乘。 我需要在输出中打印 0 - How to execute 0 when printing out the loop? I've been trying to print out the factorial in python. I need 0 to be printed in the output 我无法正确调用我的 python function - I can't work out a to call my python function properly 如何修复错误“错误:命令出错,退出状态为 1:python。” 尝试使用 pip 安装 djangorestframework-extensions 时 - How to fix error “ERROR: Command errored out with exit status 1: python.” when trying to install djangorestframework-extensions using pip 如何修复错误“错误:命令出错,退出状态为 1:python。” 尝试使用 pip 安装 fitz 模块时? - How to fix error “ERROR: Command errored out with exit status 1: python.” when trying to install fitz module using pip? 如何修复错误“错误:命令出错,退出状态为 1:python。” 尝试使用 pip 安装 pyttsx3 时 - How to fix error “ERROR: Command errored out with exit status 1: python.” when trying to install pyttsx3 using pip 当我在 python 中放入一个函数时,我的代码不起作用 - My code doesn’t work when I put in in a function in python 尝试在 Python 中制作 Watson IBM 助手。 在没有音频文件的情况下,如何修复我的代码以在 IDE Pycharm 中说出答案。 使用 VLC - Trying to make a Watson IBM assistant in Python. How can I fix my code to say the answer within the IDE Pycharm without the audio file. Using VLC 我过滤素数的功能是打印重复项,我该如何解决这个问题? - My function to filter out prime numbers is printing duplicates, how can I fix this? 我的输出与我需要的不匹配我该如何解决这个问题? - My out put doesn't match the one i need how can I fix this? 我想在 Python 中返回文件名。 我怎样才能正确地做到这一点? - I want to return file name in Python. How can I properly do it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM