Last active
November 13, 2015 16:28
-
-
Save vinovator/f9efd2a891cb30dba18a to your computer and use it in GitHub Desktop.
Persist a list of dicts using 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 | |
import pickle # To persist each dict | |
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}.p".format(num) | |
with open(os.path.join(json_path + "/" + file_name), mode="wb") as pickl: | |
pickle.dump(my_dict, pickl) | |
print("Done. Write complete") | |
my_lst = list() | |
# Read the pickle files into a list | |
for num in range (1, 100): | |
file_name = "{0}.p".format(num) | |
with open(os.path.join(json_path + "/" + file_name), mode="rb") as pickl: | |
my_lst.append(pickle.load(pickl)) | |
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