pickle - 在文件中放入多个对象? [重复]

[英]pickle - putting more than 1 object in a file? [duplicate]


This question already has an answer here:

这个问题在这里已有答案:

I have got a method which dumps a number of pickled objects (tuples, actually) into a file.

我有一个方法可以将一些pickle对象(实际上是元组)转储到一个文件中。

I do not want to put them into one list, I really want to dump several times into the same file. My problem is, how do I load the objects again? The first and second object are just one line long, so this works with readlines. But all the others are longer. naturally, if I try

我不想把它们放在一个列表中,我真的想多次转储到同一个文件中。我的问题是,如何再次加载对象?第一个和第二个对象只有一行,所以这适用于readlines。但所有其他人都更长。如果我尝试的话,很自然

myob = cpickle.load(g1.readlines()[2])

where g1 is the file, I get an EOF error because my pickled object is longer than one line. Is there a way to get just my pickled object?

其中g1是文件,我得到一个EOF错误,因为我的pickle对象长于一行。有没有办法让我的腌制物品?

3 个解决方案

#1


54  

If you pass the filehandle directly into pickle you can get the result you want.

如果您直接将文件句柄传递给pickle,您可以获得所需的结果。

import pickle

# write a file
f = open("example", "w")
pickle.dump(["hello", "world"], f)
pickle.dump([2, 3], f)
f.close()

f = open("example", "r")
value1 = pickle.load(f)
value2 = pickle.load(f)
f.close()

pickle.dump will append to the end of the file, so you can call it multiple times to write multiple values.

pickle.dump将追加到文件的末尾,因此您可以多次调用它来写入多个值。

pickle.load will read only enough from the file to get the first value, leaving the filehandle open and pointed at the start of the next object in the file. The second call will then read the second object, and leave the file pointer at the end of the file. A third call will fail with an EOFError as you'd expect.

pickle.load将从文件中读取足够的内容以获取第一个值,使文件句柄保持打开状态并指向文件中下一个对象的开头。然后第二个调用将读取第二个对象,并将文件指针保留在文件的末尾。如您所料,第三次调用将失败并出现EOFError。

Although I used plain old pickle in my example, this technique works just the same with cPickle.

虽然我在我的例子中使用了普通的老泡菜,但这种技术与cPickle的工作原理相同。

#2


16  

I think the best way is to pack your data into a single object before you store it, and unpack it after loading it. Here's an example using a tuple as the container(you can use dict also):

我认为最好的方法是在存储之前将数据打包到单个对象中,并在加载后将其解压缩。这是一个使用元组作为容器的示例(您也可以使用dict):

a = [1,2]
b = [3,4]

with open("tmp.pickle", "wb") as f:
    pickle.dump((a,b), f)

with open("tmp.pickle", "rb") as f:
    a,b = pickle.load(f) 

#3


1  

Don't try reading them back as lines of the file, justpickle.load()the number of objects you want. See my answer to the question How to save an object in Python for an example of doing that.

不要尝试将它们作为文件的行读回来,justpickle.load()你想要的对象数量。有关如何在Python中保存对象的问题,请参阅我的答案。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2013/03/17/95af3312dc61fe6b5b929a43bac97898.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com