Repository navigation
Expand file tree
/
Copy path018-python-pickle.py
More file actions
77 lines (57 loc) · 1.89 KB
/
Copy path018-python-pickle.py
File metadata and controls
77 lines (57 loc) · 1.89 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
68
69
70
71
72
73
74
75
76
77
""" MARKDOWN
---
YamlDesc: CONTENT-ARTICLE
Title: Python Pickle Serialization and De-Serialization
MetaDescription: Python Pickle Serialization and De-Serialization
MetaKeywords: Python Pickle Serialization and De-Serialization
Author: Venkata Bhattaram / tinitiate.com
ContentName: python-pickle-serialization
---
MARKDOWN """
""" MARKDOWN
# Python Pickle Serialization and De-Serialization
* Serialization is the process of converting an object state into a
binary file, this file can be stored on filesystem or transmitted across
network. or it can be persisted(stored) and later use.
* Serialization of an object is also known as deflating or marshalling.
* In Python we use the **PICKLE** module to Serialize an Object.
* In order to resurrect an Object from a Pickle file it needs to be deserialized
* Deserialization of a file into an object also known as inflating or
unmarshalling.
MARKDOWN """
# MARKDOWN ```
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('c:\\Personal\\tinitiate\\object.pickle', 'wb') as f:
pickle.dump(Obj1, f)
# DeSerialize File to Object
with open('c:\\Personal\\tinitiate\\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('c:\\Personal\\tinitiate\\dict.pickle', 'wb') as f:
pickle.dump(Dict1, f)
# DeSerialize File to Object
with open('c:\\Personal\\tinitiate\\dict.pickle', 'rb') as f:
DictFile = pickle.load(f)
print(DictFile)
# MARKDOWN ```