Skip to content

Instantly share code, notes, and snippets.

@louisswarren
Last active Nov 18, 2021
Embed
What would you like to do?
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