1

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.

What can I do to have the expected output?
Lokian
  • 93
  • 11

2 Answers2

1

You can run pickle.load() on the same file multiple times.

Like this:

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)
            loaded_file2 = pickle.load(this_file)
            return [loaded_file, loaded_file2]

    def PrintFile(self):
        for i in self.Load():
            for k, v in i.items():
                print(f"{k} {v}")


if __name__ == '__main__':
    # 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 = DataBase("test")
    db.Create(dict0)
    db.Create(dict1)
    db.PrintFile()

Gives the following output:

1 A
2 E
3 I
4 O
5 U
0 Water
1 Fire
2 Air
3 Sand
-1

You could try combining the two dictionaries before you pickle them, but this might not be what you want for your specific application. My speculation is that the .load part of db.printfile() is not loading both dictionaries and therefore is only printing/returning the first one.

Arnav Poddar
  • 354
  • 2
  • 18
  • That's the point, I can't combine because in the project I will have a class to store data like name, id, age. I've tried adapting some examples which the professor showed during the class but this I've created was the best approach for my situation. How could I make db.PrintFile() load borth dictionaries? – Lokian May 29 '19 at 00:24
  • Sorry, don't know how to help you then. Good luck – Arnav Poddar May 29 '19 at 00:24