import pprintimport jsonfrom urllib.request import urlopen # (Only used to get this example)
# Getting a JSON example for this exampler = urlopen("https://mdn.github.io/fetch-examples/fetch-json/products.json")text = r.read()
# To print itpprint.pprint(json.loads(text))
#%%
def _to_json_dict_with_strings(dictionary):"""Convert dict to dict with leafs only being strings. So it recursively makes keys to stringsif they are not dictionaries.
Use case:- saving dictionary of tensors (convert the tensors to strins!)- saving arguments from script (e.g. argparse) for it to be pretty
e.g.
"""if type(dictionary) != dict:return str(dictionary)d = {k: _to_json_dict_with_strings(v) for k, v in dictionary.items()}return d
def to_json(dic):import typesimport argparse
if type(dic) is dict:dic = dict(dic)else:dic = dic.__dict__return _to_json_dict_with_strings(dic)
def save_to_json_pretty(dic, path, mode='w', indent=4, sort_keys=True):import json
with open(path, mode) as f:json.dump(to_json(dic), f, indent=indent, sort_keys=sort_keys)
def my_pprint(dic):"""
@param dic:@return:
Note: this is not the same as pprint."""import json
# make all keys strings recursively with their naitve str functiondic = to_json(dic)# pretty printpretty_dic = json.dumps(dic, indent=4, sort_keys=True)print(pretty_dic)# print(json.dumps(dic, indent=4, sort_keys=True))# return pretty_dic
import torch# import json # results in non serializabe errors for torch.Tensorsfrom pprint import pprint
dic = {'x': torch.randn(1, 3), 'rec': {'y': torch.randn(1, 3)}}
my_pprint(dic)pprint(dic)
#%pip install treelibfrom treelib import Tree
country_tree = Tree()# Create a root nodecountry_tree.create_node("Country", "countries")
# Group by countryfor country, regions in wards_df.head(5).groupby(["CTRY17NM", "CTRY17CD"]):# Generate a node for each countrycountry_tree.create_node(country[0], country[1], parent="countries")# Group by regionfor region, las in regions.groupby(["GOR10NM", "GOR10CD"]):# Generate a node for each regioncountry_tree.create_node(region[0], region[1], parent=country[1])# Group by local authorityfor la, wards in las.groupby(['LAD17NM', 'LAD17CD']):# Create a node for each local authoritycountry_tree.create_node(la[0], la[1], parent=region[1])for ward, _ in wards.groupby(['WD17NM', 'WD17CD']):# Create a leaf node for each wardcountry_tree.create_node(ward[0], ward[1], parent=la[1])
# Output the hierarchical datacountry_tree.show()
基于此,我创建了一个将json转换为树的函数:
from treelib import Node, Tree, nodedef json_2_tree(o , parent_id=None, tree=None, counter_byref=[0], verbose=False, listsNodeSymbol='+'):if tree is None:tree = Tree()root_id = counter_byref[0]if verbose:print(f"tree.create_node({'+'}, {root_id})")tree.create_node('+', root_id)counter_byref[0] += 1parent_id = root_idif type(o) == dict:for k,v in o.items():this_id = counter_byref[0]if verbose:print(f"tree.create_node({str(k)}, {this_id}, parent={parent_id})")tree.create_node(str(k), this_id, parent=parent_id)counter_byref[0] += 1json_2_tree(v , parent_id=this_id, tree=tree, counter_byref=counter_byref, verbose=verbose, listsNodeSymbol=listsNodeSymbol)elif type(o) == list:if listsNodeSymbol is not None:if verbose:print(f"tree.create_node({listsNodeSymbol}, {counter_byref[0]}, parent={parent_id})")tree.create_node(listsNodeSymbol, counter_byref[0], parent=parent_id)parent_id=counter_byref[0]counter_byref[0] += 1for i in o:json_2_tree(i , parent_id=parent_id, tree=tree, counter_byref=counter_byref, verbose=verbose,listsNodeSymbol=listsNodeSymbol)else: #nodeif verbose:print(f"tree.create_node({str(o)}, {counter_byref[0]}, parent={parent_id})")tree.create_node(str(o), counter_byref[0], parent=parent_id)counter_byref[0] += 1return tree