django+SQLite搭建轻量级个人博客(十二)pickle模块——序列化和反序列化

pickle模块实现对象的序列化和反序列化,简单说可将对象和字符串相互转化。具体参见下面的例子:

=========序列化=========

import pickle

class test:

  name = 'hello'

obj = test()

result = pickle.dumps(obj)

print(result)

运行结果:b'\x80\x03c__main__\ntest\nq\x00)\x81q\x01.'

 

=========反序列化=========

import pickle

class test:

  name = 'hello'

 

 

str = b'\x80\x03c__main__\ntest\nq\x00)\x81q\x01.'

x = pickle.loads(str)

print(x.name)

运行结果:hello

posted @ 2019-11-07 23:56  hello_libra  阅读(230)  评论(0编辑  收藏  举报