在使用python打印JSON文件时,我的递归函数不能正确地放置制表符。我该怎么修?

2024-03-29 14:50:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我的职能

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

示例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
        }
    }

我的输出


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

似乎每一行的标签页大小都不一样,我希望输出的内容更多


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

我对使用python编程还很陌生,我想知道我犯了什么错误,导致选项卡不一致,并且输出中的间距与我预期的不一致。谢谢你的帮助

(另外,if的if语句用于我可能从JSON获取的其他类型的数据)


Tags: keyinfojsonplaystringifcurrentlevel
1条回答
网友
1楼 · 发布于 2024-03-29 14:50:25
# 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))

输出:

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

相关问题 更多 >