Created
November 16, 2015 17:13
-
-
Save vinovator/dc9ecd8fd2b808c9f39d to your computer and use it in GitHub Desktop.
Persist dicts using Json instead of pickle
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# persistListOfDicts.py | |
# Python 2.7.6 | |
import json | |
import os | |
json_path = "./JSON" | |
# Write dicts into a pickle file each | |
for num in range(1, 100): | |
my_dict = dict() | |
my_dict["loop"] = num | |
my_dict["name"] = "name - " + str(num) | |
file_name = "{0}.json".format(num) | |
with open(os.path.join(json_path + "/" + file_name), mode="wb") as log: | |
json.dump(my_dict, log) | |
print("Done. Write complete") | |
my_lst = list() | |
# Read the pickle files into a list | |
for num in range (1, 100): | |
file_name = "{0}.json".format(num) | |
with open(os.path.join(json_path + "/" + file_name), mode="rb") as log: | |
my_lst.append(json.load(log)) | |
print ("Done. Read complete") | |
for item in my_lst: | |
print(item) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment