eis/TestProject/pylib/Serialization.py

40 lines
921 B
Python
Raw Permalink Normal View History

#!/users/dsc/bin/Python3
#-*-coding: UTF-8 -*-
import pickle
root_dir="/users/dsc/model/"
def Serialization(key,value):
"""_Serialization_
将对象value序列化并保存文件key.pkl
Args:
key (_str_): 保存的文件名
value (_obj_): 需要序列化保存的对象
Returns:
_bytes_: bytes 对象序列化的结果
"""
file_name=root_dir+key+".pkl"
with open(file_name, 'wb') as file:
pickle.dump(value, file)
return pickle.dumps(value)
def Deserialization(key):
"""_Deserialization_
将保存的文件key.pkl反序列化得到对象value
Args:
key (_str_): _文件名_
Returns:
_obj_: _反序列化得到的对象_
"""
try:
file_name=root_dir+key+".pkl"
with open(file_name, 'rb') as file:
value = pickle.load(file)
return value
except:
return False