SHARE
TWEET

Untitled

a guest Sep 11th, 2019 94 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import simplejson as json
  2. from sys import argv
  3. from codecs import open
  4.  
  5.  
  6. def read_all_nodes(file):
  7.     with open(file, 'r', 'utf-8') as f:
  8.         result = f.read()
  9.         root = json.loads(result)
  10.         return root["nodes"]
  11.  
  12.  
  13. def write_nodes(nodes, current, deep, file):
  14.     children = [n for n in nodes if n["parentid"] == current]
  15.     if len(children) > 0:
  16.         for child in children:
  17.             file.write("\t" * deep)
  18.             file.write(child["topic"])
  19.             file.write("\n")
  20.             write_nodes(nodes, child["id"], deep + 1, file)
  21.  
  22.  
  23. if __name__ == "__main__":
  24.     nodes = read_all_nodes(argv[1])
  25.     root = [n for n in nodes if n["isroot"]][0]
  26.     with open("output.txt", "w", "utf-8") as f:
  27.         f.write(root["topic"])
  28.         f.write("\n")
  29.         write_nodes(nodes,root["id"],1,f)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top