-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpickle_wrapper.py
54 lines (36 loc) · 1 KB
/
pickle_wrapper.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
45
46
47
48
49
50
51
52
53
54
import pickle
import os
import pandas as pd
def pickle_it(obj, name_str):
'''
Create a pickle file in the current working directory
and store a pickle file by the name '<name_str>.pickle'
with the object serialized in it
'''
file_name = name_str + '.pickle'
with open(file_name, 'wb') as pickle_out:
pickle.dump(obj, pickle_out)
pickle_out.close()
def read_pickle(name_str):
'''
Read the pickle file named '<name_str>.pickle' to
deserialize the previously serialized object and return
it
'''
file_name = name_str + '.pickle'
obj = None
with open(file_name, 'rb') as pickle_in:
obj = pickle.load(pickle_in)
pickle_in.close()
return obj
def main():
os.chdir('C:\\Users\\praty\\Desktop\\python urllib')
x = pd.DataFrame([[1, 2, 3], [4, 5, 6]])
pickle_it(x, 'dataframe')
y = read_pickle('dataframe')
for i in range(0, 4):
print(i, type(y))
print(y)
print(type(y))
if __name__ == '__main__':
main()