I must to use pickle to store some data. I've created a class to handle the i/o of the data, one the functions is resposible for loading the data and print all the informations, but when I call the function to print out the data, it only interates through the first dictionary. All the inputs are being saved (as I openned the pickle file and could see some data related to other inputs).
I've checked some of the stackoverflow questions like: Iterating over dictionaries using 'for' loops and Best way to loop through multiple dictionaries in Python but none of the examples gave me the expected output.
This is the DataBase class which is resposible to handle pickle:
import pickle
from pathlib import Path
class DataBase:
def __init__(self, out_file):
self.out_file = out_file
def Create(self, file_name):
if not Path(self.out_file).exists():
with open(self.out_file, "w+b") as this_file:
new_file = pickle.dump(file_name, this_file, pickle.HIGHEST_PROTOCOL)
elif Path(self.out_file).exists():
self.Update(file_name)
else:
print("Unknown error!")
def Update(self, file_name):
with open(self.out_file, "a+b") as this_file:
updated_file = pickle.dump(file_name, this_file, pickle.HIGHEST_PROTOCOL)
def Load(self):
with open(self.out_file, "rb") as this_file:
loaded_file = pickle.load(this_file)
return loaded_file
def PrintFile(self):
for k, v in self.Load().items():
print(f"{k} {v}")
This is the code I've written for testing the class:
from DataBase import DataBase as DB
dict0 = {1:'A', 2:'E', 3:'I', 4:'O', 5:'U'}
dict1 = {0:"Water", 1:"Fire", 2:"Air", 3:"Sand"}
db = DB("test")
db.Create(dict0)
db.Create(dict1)
db.PrintFile()
I expected the output to be:
1 A
2 E
3 I
4 O
5 U
0 Water
1 Fire
2 Air
3 Sand
However the output I get is:
1 A
2 E
3 I
4 O
5 U
The whole information is being saved, when I open the pickle file with a text ediot, I can see some o the information stored there:
��#}�(K�A�K�E�K�I�K�O�K�U�u.��)}�(K�Water�K�Fire�K�Air�K�Sand�u
We can see for example Air in the file.