-
Notifications
You must be signed in to change notification settings - Fork 0
/
serialization.py
44 lines (28 loc) · 1.06 KB
/
serialization.py
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
33
34
35
36
37
38
39
40
41
42
43
44
import pickle
a_dict = dict()
a_dict['EUR'] = {'code':'Euro', 'symbol': '€'}
a_dict['GBP'] = {'code':'Pounds sterling', 'symbol': '£'}
a_dict['USD'] = {'code':'US dollar', 'symbol': '$'}
a_dict['JPY'] = {'code':'Japanese yen', 'symbol': '¥'}
a_list = ['a', 123, [10, 100, 1000]]
with open('multidata.pckl', 'wb') as file_out:
pickle.dump(a_dict, file_out)
pickle.dump(a_list, file_out)
import pickle
with open('multidata.pckl', 'rb') as file_in:
data1 = pickle.load(file_in)
data2 = pickle.load(file_in)
print(type(data1))
print(data1)
print(type(data2))
print(data2)
#=============================#
import pickle
a_list = ['a', 123, [10, 100, 1000]]
bytes = pickle.dumps(a_list)
print('Intermediate object type, used to preserve data:', type(bytes),bytes)
# now pass 'bytes' to appropriate driver
# therefore when you receive a bytes object from an appropriate driver you can deserialize it
b_list = pickle.loads(bytes)
print('A type of deserialized object:', type(b_list),b_list)
print('Contents:', b_list)