Repository navigation
Expand file tree
/
Copy pathpickle.py
More file actions
67 lines (42 loc) · 1.28 KB
/
Copy pathpickle.py
File metadata and controls
67 lines (42 loc) · 1.28 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# Title: Python Pickle Serialization
# Notes:
# Serialization is the process of converting an object state
# into a string format file or memory buffer,
# In order to resurrected for later use for a later use or
# transmitted across a network
# serializing an object is also known as deflating or marshalling
# deserialization a string or file into an object also known as inflating or unmarshalling
import pickle;
#############################
# Serialization of an Object
#############################
# Create a Class
class PickleTest():
a=0
b=0
def __init__(self,i_a,i_b):
self.a=i_a
self.b=i_b
# Create an Object
Obj1 = PickleTest(1,2)
# Serialize an Object
with open('object.pickle', 'wb') as f:
pickle.dump(Obj1, f)
# DeSerialize File to Object
with open('object.pickle', 'rb') as f:
ObjFile = pickle.load(f)
print(ObjFile)
print(ObjFile.a)
print(ObjFile.b)
################################
# Serialization of a Dictionary
################################
# Create a Dict
Dict1={'APPLE':'FRUIT', 'POTATO':'ROOT', 'OKRA':'VEGETABLE'}
# Serialize an Dict
with open('dict.pickle', 'wb') as f:
pickle.dump(Dict1, f)
# DeSerialize File to Object
with open('dict.pickle', 'rb') as f:
DictFile = pickle.load(f)
print(DictFile)