Skip to content

Instantly share code, notes, and snippets.

@pvavra
Last active December 5, 2019 19:05
Show Gist options
  • Save pvavra/a0e831b0e2184aa6dc2cda903a35ba73 to your computer and use it in GitHub Desktop.
Save pvavra/a0e831b0e2184aa6dc2cda903a35ba73 to your computer and use it in GitHub Desktop.
studyspec.json unpack/repack scripts
#!/usr/bin/env python
import sys
import json as js
import codecs
in_file = sys.argv[1]
if len(sys.argv) > 2:
out_file_json = sys.argv[2]
else:
out_file_json = in_file
with open(in_file, mode='rb') as f:
specs = js.load(f)
enc = js.JSONEncoder(separators=(',', ':'))
with open(out_file_json, "wb") as f:
jwriter = codecs.getwriter('utf-8')(f)
for spec in specs:
line = enc.encode(spec)
jwriter.writelines(line)
f.write(b'\n')
#!/usr/bin/env python
import sys
import json as js
import codecs
in_file = sys.argv[1]
if len(sys.argv) > 2:
out_file_json = sys.argv[2]
else:
out_file_json = in_file
dec = js.JSONDecoder()
enc = js.JSONEncoder(sort_keys=False, indent=4)
content = "["
with open(in_file, mode='rb') as f:
jreader = codecs.getreader('utf-8')(f)
for line in jreader:
unpacked = dec.decode(line)
content += enc.encode(unpacked)
content += ',\n'
# remove last comma
content = content[:-2]
content += "]\n"
with open(out_file_json, "w") as write_file:
write_file.write(content)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment