Skip to content

Instantly share code, notes, and snippets.

@louisswarren
Last active November 18, 2021 23:53
Show Gist options
  • Save louisswarren/f13ed726faf2c4b9b7e9f8cf5e88c59a to your computer and use it in GitHub Desktop.
Save louisswarren/f13ed726faf2c4b9b7e9f8cf5e88c59a to your computer and use it in GitHub Desktop.
Convert between pickle and json sequences
import sys
import pickle
import json
def usage():
print("Usage:")
print(f"\t{sys.argv[0]} --tojson [PICKLE FILES]")
print(f"\t{sys.argv[0]} --topickle [JSON FILES]")
JD = json.JSONDecoder()
def pickle_to_json(pklf, jsonf):
try:
while True:
json.dump(pickle.load(pklf), jsonf)
except EOFError:
return
def json_to_pickle(jsonf, pklf):
jdata = jsonf.read().strip()
idx = 0
while idx < len(jdata):
obj, s = JD.raw_decode(jdata[idx:])
pickle.dump(obj, pklf)
idx += s
def pickle_to_json_all(filenames):
for filename in filenames:
if filename.endswith('.pkl'):
outname = filename[:-4] + '.json'
else:
outname = filename
with open(filename, 'rb') as pklf, open(outname, 'w') as jsonf:
pickle_to_json(pklf, jsonf)
def json_to_pickle_all(filenames):
for filename in filenames:
if filename.endswith('.json'):
outname = filename[:-5] + '.pkl'
else:
outname = filename
with open(filename) as jsonf, open(outname, 'wb') as pklf:
json_to_pickle(jsonf, pklf)
if __name__ == '__main__':
if len(sys.argv) == 1:
usage()
elif sys.argv[1] == '--tojson':
pickle_to_json_all(sys.argv[2:])
elif sys.argv[1] == '--topickle':
json_to_pickle_all(sys.argv[2:])
else:
usage()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment