字典、列表嵌套體輸出字典鍵樹狀結構

不廢話了,直接上代碼

def zjh_print(temp, indent=0):
    if type(temp) == dict:
        for key, value in temp.items():
            print('')
            print('|'*indent, end='')
            print(key, end='')
            zjh_print(value, indent + 1)
    elif type(temp) == list:
        # for data in temp:
        print('->list', end='')
        print('|'*(indent-1), end='')
        zjh_print(temp[0], indent)

我手上的一個這樣的嵌套體,

zjh_print(artist_dict)

輸出結果如下(首行爲空行)


name
mbid
streamable
ontour
listeners
playcount
bio_published
bio_summary
bio_content
similar_artists->list
|name
|mbid
|match
|streamable
tags->list
|name
|count
tracks->list
|name
|play count
|listeners
|mbid
|streamable
|rank

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章