Advertisement
Guest User

Untitled

a guest
Oct 26th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. import sys
  2. import pandas as pd
  3. import ujson as json
  4. from collections import defaultdict
  5.  
  6. def item_info(item):
  7. if (item["status"] == 1) and (item["visibility"] in [2, 3, 4]):
  8. return dict(id=item["id"], level=item["level"], name=item["name"], status=item["status"],
  9. visibility=item["visibility"], parent_id=None)
  10.  
  11. return None
  12.  
  13. def populate_entry(tree, item, parent_id):
  14. # add item to the tree and return id for use as parent_id in next level
  15. linfo = item_info(item)
  16.  
  17. if linfo:
  18. linfo["parent_id"] = parent_id
  19. tree.append(linfo)
  20. return linfo["id"]
  21.  
  22. else:
  23. return None
  24.  
  25. def read_catalog(infile):
  26. category_tree = []
  27.  
  28. with open(infile) as f:
  29. data = json.loads(f.read())
  30.  
  31. for l0 in data:
  32. p_id_l1 = populate_entry(category_tree, l0, None)
  33.  
  34. if p_id_l1:
  35. for l1 in l0["items"]:
  36. p_id_l2 = populate_entry(category_tree, l1, p_id_l1)
  37.  
  38. if p_id_l2:
  39. for l2 in l1["items"]:
  40. p_id_l3 = populate_entry(category_tree, l2, p_id_l2)
  41.  
  42. if p_id_l3:
  43. for l3 in l2["items"]:
  44. p_id_l4 = populate_entry(category_tree, l3, p_id_l3)
  45.  
  46. if p_id_l4:
  47. for l4 in l3["items"]:
  48. p_id_l5 = populate_entry(category_tree, l4, p_id_l4)
  49.  
  50. df = pd.DataFrame(category_tree)
  51. df.to_csv("test.csv", index=False, encoding='utf-8')
  52.  
  53. if __name__ == '__main__':
  54. infile = sys.argv[1]
  55. read_catalog(infile)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement