Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 17 additions & 15 deletions scripts/txt_to_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()