保存和加载对象并使用pickle [英] Saving and loading objects and using pickle

查看:31
本文介绍了保存和加载对象并使用pickle的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 pickle 模块保存和加载对象.
首先我声明我的对象:

<预><代码>>>>类水果:通过...>>>香蕉 = 水果()>>>香蕉颜色 = '黄色'>>>香蕉值 = 30

之后,我打开了一个名为Fruits.obj"的文件(之前我创建了一个新的 .txt 文件并将其重命名为Fruits.obj"):

<预><代码>>>>进口泡菜>>>filehandler = open(b"Fruits.obj","wb")>>>pickle.dump(香蕉,文件处理程序)

完成此操作后,我关闭会话并开始一个新会话,然后放置下一个(尝试访问它应该保存的对象):

file = open("Fruits.obj",'r')object_file = pickle.load(file)

但我收到了这条消息:

回溯(最近一次调用最后一次):文件<stdin>",第 1 行,在 <module> 中文件C:Python31libpickle.py",第 1365 行,加载中编码=编码,错误=错误).load()ValueError: read() 从底层流没有返回字节

我不知道该怎么做,因为我不明白这条消息.有谁知道我如何加载我的对象香蕉"?谢谢!

正如你们中的一些人所暗示的那样:

<预><代码>>>>进口泡菜>>>file = open("Fruits.obj",'rb')

没有问题,但我提出的下一个是:

<预><代码>>>>object_file = pickle.load(file)

我有错误:

回溯(最近一次调用最后一次):文件<stdin>",第 1 行,在 <module> 中文件C:Python31libpickle.py",第 1365 行,加载中编码=编码,错误=错误).load()EOF错误

解决方案

至于你的第二个问题:

 回溯(最近一次调用):文件<stdin>",第 1 行,在 <module> 中.文件C:Python31libpickle.py",行1365,在加载编码=编码中,错误=错误).load() EOFError

读取文件内容后,文件指针将位于文件末尾 - 将不再有数据可读取.您必须倒带文件,以便再次从头读取:

file.seek(0)

您通常想做的是使用上下文管理器打开文件并从中读取数据.这样,文件将在块执行完毕后自动关闭,这也有助于您将文件操作组织成有意义的块.

最后,cPickle 是用 C 语言实现的 pickle 模块的更快实现.所以:

在 [1] 中:将 _pickle 导入为 cPickle在 [2] 中:d = {a":1,b":2}在 [4] 中:以 open(r"someobject.pickle", "wb") 作为 output_file:...: cPickle.dump(d, output_file)...:# pickle_file 将在此时关闭,防止您进一步访问它在 [5] 中:以 open(r"someobject.pickle", "rb") 作为 input_file:...: e = cPickle.load(input_file)...:在 [7] 中:打印 e------>打印(e){'a':1,'b':2}

I´m trying to save and load objects using pickle module.
First I declare my objects:

>>> class Fruits:pass
...
>>> banana = Fruits()

>>> banana.color = 'yellow'
>>> banana.value = 30

After that I open a file called 'Fruits.obj'(previously I created a new .txt file and I renamed 'Fruits.obj'):

>>> import pickle
>>> filehandler = open(b"Fruits.obj","wb")
>>> pickle.dump(banana,filehandler)

After do this I close my session and I began a new one and I put the next (trying to access to the object that it supposed to be saved):

file = open("Fruits.obj",'r')
object_file = pickle.load(file)

But I have this message:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:Python31libpickle.py", line 1365, in load
encoding=encoding, errors=errors).load()
ValueError: read() from the underlying stream did notreturn bytes

I don´t know what to do because I don´t understand this message. Does anyone know How I can load my object 'banana'? Thank you!

EDIT: As some of you have sugested I put:

>>> import pickle
>>> file = open("Fruits.obj",'rb')

There were no problem, but the next I put was:

>>> object_file = pickle.load(file)

And I have error:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:Python31libpickle.py", line 1365, in load
encoding=encoding, errors=errors).load()
EOFError

解决方案

As for your second problem:

 Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
 File "C:Python31libpickle.py", line
 1365, in load encoding=encoding,
 errors=errors).load() EOFError

After you have read the contents of the file, the file pointer will be at the end of the file - there will be no further data to read. You have to rewind the file so that it will be read from the beginning again:

file.seek(0)

What you usually want to do though, is to use a context manager to open the file and read data from it. This way, the file will be automatically closed after the block finishes executing, which will also help you organize your file operations into meaningful chunks.

Finally, cPickle is a faster implementation of the pickle module in C. So:

In [1]: import _pickle as cPickle

In [2]: d = {"a": 1, "b": 2}

In [4]: with open(r"someobject.pickle", "wb") as output_file:
   ...:     cPickle.dump(d, output_file)
   ...:

# pickle_file will be closed at this point, preventing your from accessing it any further

In [5]: with open(r"someobject.pickle", "rb") as input_file:
   ...:     e = cPickle.load(input_file)
   ...:

In [7]: print e
------> print(e)
{'a': 1, 'b': 2}

这篇关于保存和加载对象并使用pickle的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆