Created
October 25, 2016 23:17
-
-
Save sagunb/f94356c3e7bb435aaef4fa5bf9e93e47 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment