如何对pickle字节数据进行unpickled?

时间:2015-05-12 19:02:10

标签: python pickle

如何从BytesIO对象写入和回读pickle数据?

我试过了:

import io
import cPickle as pickle

s1 = "foo"
bytes_io = io.BytesIO()
pickle.dump(s1, bytes_io, pickle.HIGHEST_PROTOCOL)
s2 = pickle.load(bytes_io)

给出了pickle.load行:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
EOFError

1 个答案:

答案 0 :(得分:5)

您刚刚在pickle.load之前没有回复缓冲区:bytes_io.seek(0)

可能你不想回到缓冲区的前面,而只是回到你的腌制数据的开头。然后在酸洗之前用bytes_io.tell()读出流位置并寻找该位置而不是0