Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- import pandas as pd
- import ujson as json
- from collections import defaultdict
- def item_info(item):
- if (item["status"] == 1) and (item["visibility"] in [2, 3, 4]):
- return dict(id=item["id"], level=item["level"], name=item["name"], status=item["status"],
- visibility=item["visibility"], parent_id=None)
- return None
- def populate_entry(tree, item, parent_id):
- # add item to the tree and return id for use as parent_id in next level
- linfo = item_info(item)
- if linfo:
- linfo["parent_id"] = parent_id
- tree.append(linfo)
- return linfo["id"]
- else:
- return None
- def read_catalog(infile):
- category_tree = []
- with open(infile) as f:
- data = json.loads(f.read())
- for l0 in data:
- p_id_l1 = populate_entry(category_tree, l0, None)
- if p_id_l1:
- for l1 in l0["items"]:
- p_id_l2 = populate_entry(category_tree, l1, p_id_l1)
- if p_id_l2:
- for l2 in l1["items"]:
- p_id_l3 = populate_entry(category_tree, l2, p_id_l2)
- if p_id_l3:
- for l3 in l2["items"]:
- p_id_l4 = populate_entry(category_tree, l3, p_id_l3)
- if p_id_l4:
- for l4 in l3["items"]:
- p_id_l5 = populate_entry(category_tree, l4, p_id_l4)
- df = pd.DataFrame(category_tree)
- df.to_csv("test.csv", index=False, encoding='utf-8')
- if __name__ == '__main__':
- infile = sys.argv[1]
- read_catalog(infile)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement