字符串序列化是将对象状态写入字节流的过程。 在python中,pickle
库用于启用序列化。 该模块包含一个用于序列化和反序列化python对象结构的强大算法。 “pickling”是将python对象层次转换为字节流的过程,“unpickling”是相反的过程。
pickle模块的示列如下 -
import pickle
#here's an example dict
grades = { 'alice': 89, 'bob': 72, 'charles': 87 }
#use dumps to convert the object to a serialized string
serial_grades = pickle.dumps( grades )
print(serial_grades)
#use loads to de-serialize an object
received_grades = pickle.loads( serial_grades )
print(received_grades)
执行上面示例代码,得到以下结果 -