python

关注公众号 jb51net

关闭
首页 > 脚本专栏 > python > Python中pickle模块的使用示例

Python中的数据对象持久化存储模块pickle的使用示例

作者:oldj

这篇文章主要介绍了Python中的数据对象持久化存储模块pickle的使用示例,重点讲解了pickle中模块中对象持久化和文件读取的相关方法,需要的朋友可以参考下

Python中可以使用 pickle 模块将对象转化为文件保存在磁盘上,在需要的时候再读取并还原。具体用法如下:
pickle是Python库中常用的序列化工具,可以将内存对象以文本或二进制格式导出为字符串,或者写入文档。后续可以从字符或文档中还原为内存对象。新版本的Python中用c重新实现了一遍,叫cPickle,性能更高。 下面的代码演示了pickle库的常用接口用法,非常简单:

import cPickle as pickle

# dumps and loads
# 将内存对象dump为字符串,或者将字符串load为内存对象
def test_dumps_and_loads():
  t = {'name': ['v1', 'v2']}
  print t

  o = pickle.dumps(t)
  print o
  print 'len o: ', len(o)

  p = pickle.loads(o)
  print p

 

# 关于HIGHEST_PROTOCOL参数,pickle 支持3种protocol,0、1、2:
# http://stackoverflow.com/questions/23582489/python-pickle-protocol-choice
# 0:ASCII protocol,兼容旧版本的Python
# 1:binary format,兼容旧版本的Python
# 2:binary format,Python2.3 之后才有,更好的支持new-sytle class
def test_dumps_and_loads_HIGHEST_PROTOCOL():
  print 'HIGHEST_PROTOCOL: ', pickle.HIGHEST_PROTOCOL

  t = {'name': ['v1', 'v2']}
  print t

  o = pickle.dumps(t, pickle.HIGHEST_PROTOCOL)
  print 'len o: ', len(o)

  p = pickle.loads(o)
  print p


# new-style class
def test_new_sytle_class():
  class TT(object):
    def __init__(self, arg, **kwargs):
      super(TT, self).__init__()
      self.arg = arg
      self.kwargs = kwargs

    def test(self):
      print self.arg
      print self.kwargs

  # ASCII protocol
  t = TT('test', a=1, b=2)
  o1 = pickle.dumps(t)
  print o1
  print 'o1 len: ', len(o1)
  p = pickle.loads(o1)
  p.test()

  # HIGHEST_PROTOCOL对new-style class支持更好,性能更高
  o2 = pickle.dumps(t, pickle.HIGHEST_PROTOCOL)
  print 'o2 len: ', len(o2)
  p = pickle.loads(o2)
  p.test()


# dump and load
# 将内存对象序列化后直接dump到文件或支持文件接口的对象中
# 对于dump,需要支持write接口,接受一个字符串作为输入参数,比如:StringIO
# 对于load,需要支持read接口,接受int输入参数,同时支持readline接口,无输入参数,比如StringIO

# 使用文件,ASCII编码
def test_dump_and_load_with_file():
  t = {'name': ['v1', 'v2']}

  # ASCII format
  with open('test.txt', 'w') as fp:
    pickle.dump(t, fp)

  with open('test.txt', 'r') as fp:
    p = pickle.load(fp)
    print p


# 使用文件,二进制编码
def test_dump_and_load_with_file_HIGHEST_PROTOCOL():
  t = {'name': ['v1', 'v2']}
  with open('test.bin', 'wb') as fp:
    pickle.dump(t, fp, pickle.HIGHEST_PROTOCOL)

  with open('test.bin', 'rb') as fp:
    p = pickle.load(fp)
    print p


# 使用StringIO,二进制编码
def test_dump_and_load_with_StringIO():
  import StringIO

  t = {'name': ['v1', 'v2']}

  fp = StringIO.StringIO()
  pickle.dump(t, fp, pickle.HIGHEST_PROTOCOL)

  fp.seek(0)
  p = pickle.load(fp)
  print p

  fp.close()


# 使用自定义类
# 这里演示用户自定义类,只要实现了write、read、readline接口,
# 就可以用作dump、load的file参数
def test_dump_and_load_with_user_def_class():
  import StringIO

  class FF(object):
    def __init__(self):
      self.buf = StringIO.StringIO()

    def write(self, s):
      self.buf.write(s)
      print 'len: ', len(s)

    def read(self, n):
      return self.buf.read(n)

    def readline(self):
      return self.buf.readline()

    def seek(self, pos, mod=0):
      return self.buf.seek(pos, mod)

    def close(self):
      self.buf.close()

  fp = FF()
  t = {'name': ['v1', 'v2']}
  pickle.dump(t, fp, pickle.HIGHEST_PROTOCOL)

  fp.seek(0)
  p = pickle.load(fp)
  print p

  fp.close()


# Pickler/Unpickler
# Pickler(file, protocol).dump(obj) 等价于 pickle.dump(obj, file[, protocol])
# Unpickler(file).load() 等价于 pickle.load(file)
# Pickler/Unpickler 封装性更好,可以很方便的替换file
def test_pickler_unpickler():
  t = {'name': ['v1', 'v2']}

  f = file('test.bin', 'wb')
  pick = pickle.Pickler(f, pickle.HIGHEST_PROTOCOL)
  pick.dump(t)
  f.close()

  f = file('test.bin', 'rb')
  unpick = pickle.Unpickler(f)
  p = unpick.load()
  print p
  f.close()


pickle.dump(obj, file[, protocol])
这是将对象持久化的方法,参数的含义分别为:

对象被持久化后怎么还原呢?pickle 模块也提供了相应的方法,如下:

pickle.load(file)
只有一个参数 file ,对应于上面 dump 方法中的 file 参数。这个 file 必须是一个拥有一个能接收一个整数为参数的 read() 方法以及一个不接收任何参数的 readline() 方法,并且这两个方法的返回值都应该是字符串。这可以是一个打开为读的文件对象、StringIO 对象或其他任何满足条件的对象。

下面是一个基本的用例:

# -*- coding: utf-8 -*-

import pickle
# 也可以这样:
# import cPickle as pickle

obj = {"a": 1, "b": 2, "c": 3}

# 将 obj 持久化保存到文件 tmp.txt 中
pickle.dump(obj, open("tmp.txt", "w"))

# do something else ...

# 从 tmp.txt 中读取并恢复 obj 对象
obj2 = pickle.load(open("tmp.txt", "r"))

print obj2

# -*- coding: utf-8 -*-
 
import pickle
# 也可以这样:
# import cPickle as pickle
 
obj = {"a": 1, "b": 2, "c": 3}
 
# 将 obj 持久化保存到文件 tmp.txt 中
pickle.dump(obj, open("tmp.txt", "w"))
 
# do something else ...
 
# 从 tmp.txt 中读取并恢复 obj 对象
obj2 = pickle.load(open("tmp.txt", "r"))
 
print obj2


不过实际应用中,我们可能还会有一些改进,比如用 cPickle 来代替 pickle ,前者是后者的一个 C 语言实现版本,拥有更快的速度,另外,有时在 dump 时也会将第三个参数设为 True 以提高压缩比。再来看下面的例子:

# -*- coding: utf-8 -*-

import cPickle as pickle
import random
import os

import time

LENGTH = 1024 * 10240

def main():
 d = {}
 a = []
 for i in range(LENGTH):
 a.append(random.randint(0, 255))

 d["a"] = a

 print "dumping..."

 t1 = time.time()
 pickle.dump(d, open("tmp1.dat", "wb"), True)
 print "dump1: %.3fs" % (time.time() - t1)

 t1 = time.time()
 pickle.dump(d, open("tmp2.dat", "w"))
 print "dump2: %.3fs" % (time.time() - t1)

 s1 = os.stat("tmp1.dat").st_size
 s2 = os.stat("tmp2.dat").st_size

 print "%d, %d, %.2f%%" % (s1, s2, 100.0 * s1 / s2)

 print "loading..."

 t1 = time.time()
 obj1 = pickle.load(open("tmp1.dat", "rb"))
 print "load1: %.3fs" % (time.time() - t1)

 t1 = time.time()
 obj2 = pickle.load(open("tmp2.dat", "r"))
 print "load2: %.3fs" % (time.time() - t1)


if __name__ == "__main__":
 main()

# -*- coding: utf-8 -*-
 
import cPickle as pickle
import random
import os
 
import time
 
LENGTH = 1024 * 10240
 
def main():
 d = {}
 a = []
 for i in range(LENGTH):
 a.append(random.randint(0, 255))
 
 d["a"] = a
 
 print "dumping..."
 
 t1 = time.time()
 pickle.dump(d, open("tmp1.dat", "wb"), True)
 print "dump1: %.3fs" % (time.time() - t1)
 
 t1 = time.time()
 pickle.dump(d, open("tmp2.dat", "w"))
 print "dump2: %.3fs" % (time.time() - t1)
 
 s1 = os.stat("tmp1.dat").st_size
 s2 = os.stat("tmp2.dat").st_size
 
 print "%d, %d, %.2f%%" % (s1, s2, 100.0 * s1 / s2)
 
 print "loading..."
 
 t1 = time.time()
 obj1 = pickle.load(open("tmp1.dat", "rb"))
 print "load1: %.3fs" % (time.time() - t1)
 
 t1 = time.time()
 obj2 = pickle.load(open("tmp2.dat", "r"))
 print "load2: %.3fs" % (time.time() - t1)
 
 
if __name__ == "__main__":
 main()


在我的电脑上执行结果为:

dumping…
dump1: 1.297s
dump2: 4.750s
20992503, 68894198, 30.47%
loading…
load1: 2.797s
load2: 10.125s

可以看到,dump 时如果指定了 protocol 为 True,压缩过后的文件的大小只有原来的文件的 30% ,同时无论在 dump 时还是 load 时所耗费的时间都比原来少。因此,一般来说,可以建议把这个值设为 True 。

另外,pickle 模块还提供 dumps 和 loads 两个方法,用法与上面的 dump 和 load 方法类似,只是不需要输入 file 参数,输入及输出都是字符串对象,有些场景中使用这两个方法可能更为方便。

您可能感兴趣的文章:
阅读全文