-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserialization.py
More file actions
53 lines (39 loc) · 1.07 KB
/
serialization.py
File metadata and controls
53 lines (39 loc) · 1.07 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
# serialization and deserialization in python
# marshall module
# pickle module
# JSON module
import json
import pickle
import marshal
data = {'name': 'sreekanth', 'age': 22, 'address': 'hyderabad'}
bytes = marshal.dumps(data)
print('After serialization:', bytes)
new_data = marshal.loads(bytes)
print('After deserialization:', new_data)
# pickle module
data = {'st_name': 'Sunny', 'st_id': '9607', 'st_add': 'Nasik'}
with open('data.pickle', 'wb') as f1:
pickle.dump(data, f1)
print('pickling completed')
with open('data.pickle', 'rb') as f2:
print('unpickling the data')
data = pickle.load(f2)
print(data)
# importing the module
# JSON string
students = '{"id":"9607", "name": "Sunny", "department":"Computer"}'
# convert string to Python dict
student_dict = json.loads(students)
print(student_dict)
print(student_dict['name'])
print('Deserialization Completed.')
# importing the module
data = {
"id": "877",
"name": "Mayur",
"department": "Comp"
}
# Serializing json
json_object = json.dumps(data)
print(json_object)
print('Serialization Completed.')