簡體   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?

我的功能

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 中獲取的其他類型的數據)

# 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM