Chapter 7: More file handling

Pickling time!

face Josiah Wang

Just like with JSON files, you pickle with pickle.dump(obj, file), and unpickle with pickle.load(file). That’s it, really! Just remember that file needs to be a binary file.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
import pickle

courses = {
   70053: {"lecturer": "Josiah Wang", "title": "Python Programming"}, 
   70051: {"lecturer": "Robert Craven", "title": "Symbolic AI"}
}

# Save courses to disk. Note binary mode!
with open("courses.pkl", "wb") as file:
    pickle.dump(courses, file)

# Load courses from disk. Again, it is a binary file!
with open("courses.pkl", "rb") as file: 
    pickled_courses = pickle.load(file)

print(pickled_courses)
## {70053: {'lecturer': 'Josiah Wang', 'title': 'Python Programming'}, 
## 70051: {'lecturer': 'Robert Craven', 'title': 'Symbolic AI'}} 

print(type(pickled_courses)) ## <class 'dict'> 

print(courses == pickled_courses) ## True

Exercise

Here is a quick exercise. Your task:

  • pickle vector in the code below and save it as vectors.pkl.
  • then load the vector back from the file you just saved, and examine that the results are the same as expected.

Note that this time you are pickling something more complex: a list of Vectors (your custom class).

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import pickle

class Vector: 
    def __init__(self, x, y): 
        self.x = x 
        self.y = y

    def __str__(self): 
        return f"Vector ({self.x}, {self.y})"

    def __repr__(self):
        """ This makes the unique string representation
            of the object instance look more readable
        """
        return str(self)


vector1 = Vector(2, 3) 
vector2 = Vector(4, 3) 
vector = [vector1, vector2] 

# TODO: Save vector to disk.
????

# TODO: Load pickled file that you saved earlier from disk
pickled_vectors = ????

print(pickled_vectors)  ## [Vector (2, 3), Vector (4, 3)]

print(type(pickled_vectors))  ## <class 'list'>

A possible solution:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import pickle

class Vector: 
    def __init__(self, x, y): 
        self.x = x 
        self.y = y

    def __str__(self): 
        return f"Vector ({self.x}, {self.y})"

    def __repr__(self):
        """ This makes the unique string representation
            of the object instance look more readable
        """
        return str(self)


vector1 = Vector(2, 3) 
vector2 = Vector(4, 3) 
vector = [vector1, vector2] 

# Save vector to disk.
with open("vectors.pkl", "wb") as file: 
    pickle.dump(vector, file)

# Load pickled file that you saved earlier from disk
with open("vectors.pkl", "rb") as file: 
    pickled_vectors = pickle.load(file)

print(pickled_vectors)  ## [Vector (2, 3), Vector (4, 3)]

print(type(pickled_vectors))  ## <class 'list'>