Skip to content

Instantly share code, notes, and snippets.

@mysticBliss
Forked from hailiang-wang/pkl_to_json.py
Created November 27, 2017 07:29
Show Gist options
  • Save mysticBliss/7190c9d77455e52135946fae3808c20b to your computer and use it in GitHub Desktop.
Save mysticBliss/7190c9d77455e52135946fae3808c20b to your computer and use it in GitHub Desktop.
Convert python pickle file to json
#!/usr/local/bin/python3
'''
Convert a pkl file into json file
'''
import sys
import os
import _pickle as pickle
import json
def convert_dict_to_json(file_path):
with open(file_path, 'rb') as fpkl, open('%s.json' % file_path, 'w') as fjson:
data = pickle.load(fpkl)
json.dump(data, fjson, ensure_ascii=False, sort_keys=True, indent=4)
def main():
if sys.argv[1] and os.path.isfile(sys.argv[1]):
file_path = sys.argv[1]
print("Processing %s ..." % file_path)
convert_dict_to_json(file_path)
else:
print("Usage: %s abs_file_path" % (__file__))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment