Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import simplejson as json
- from sys import argv
- from codecs import open
- def read_all_nodes(file):
- with open(file, 'r', 'utf-8') as f:
- result = f.read()
- root = json.loads(result)
- return root["nodes"]
- def write_nodes(nodes, current, deep, file):
- children = [n for n in nodes if n["parentid"] == current]
- if len(children) > 0:
- for child in children:
- file.write("\t" * deep)
- file.write(child["topic"])
- file.write("\n")
- write_nodes(nodes, child["id"], deep + 1, file)
- if __name__ == "__main__":
- nodes = read_all_nodes(argv[1])
- root = [n for n in nodes if n["isroot"]][0]
- with open("output.txt", "w", "utf-8") as f:
- f.write(root["topic"])
- f.write("\n")
- write_nodes(nodes,root["id"],1,f)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement