From 7de19fb837249afe0f65554961ed9a29035de550 Mon Sep 17 00:00:00 2001 From: Aaron Critchley Date: Thu, 20 Dec 2018 18:52:19 +0000 Subject: [PATCH] Updating txt_to_json.py to run with guard --- scripts/txt_to_json.py | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/scripts/txt_to_json.py b/scripts/txt_to_json.py index a43bb89..f00c4b8 100644 --- a/scripts/txt_to_json.py +++ b/scripts/txt_to_json.py @@ -4,25 +4,27 @@ import json -with open('../blns.txt', 'r') as f: +def txt_to_json(): + with open('../blns.txt', 'r') as f: - # put all lines in the file into a Python list - content = f.readlines() + # put all lines in the file into a Python list + content = f.readlines() - # above line leaves trailing newline characters; strip them out - content = [x.strip('\n') for x in content] + # above line leaves trailing newline characters; strip them out + content = [x.strip('\n') for x in content] - # remove empty-lines and comments - content = [x for x in content if x and not x.startswith('#')] + # remove empty-lines and comments + content = [x for x in content if x and not x.startswith('#')] - # insert empty string since all are being removed - content.insert(0, "") + # insert empty string since all are being removed + content.insert(0, "") - # special case: convert "\" to "\\" for valid JSON - #content = map(lambda x: x.replace('\','\\'), content) + # special case: convert "\" to "\\" for valid JSON + # content = map(lambda x: x.replace('\','\\'), content) -with open('../blns.json', 'wb') as f: + with open('../blns.json', 'wb') as f: + # write JSON to file; note the ensure_ascii parameter + json.dump(content, f, indent=2, ensure_ascii=False) - # write JSON to file; note the ensure_ascii parameter - json.dump(content, f, indent=2, ensure_ascii=False) - +if __name__ == '__main__': + txt_to_json()