Python 如何读取pickle文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35067957/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 15:56:47  来源:igfitidea点击:

How to read pickle file?

pythonpickle

提问by Kenenbek Arzymatov

I created some data and stored it several times like this:

我创建了一些数据并像这样存储了几次:

with open('filename', 'a') as f:
        pickle.dump(data, f)

Every time the size of file increased, but when I open file

每次文件大小增加,但是当我打开文件时

with open('filename', 'rb') as f:
    x = pickle.load(f)

I can see only data from the last time. How can I correctly read file?

我只能看到上次的数据。如何正确读取文件?

采纳答案by jsbueno

Pickle serializes a single object at a time, and reads back a single object - the pickled data is recorded in sequence on the file.

Pickle 一次序列化一个对象,读回一个对象——pickle 的数据按顺序记录在文件中。

If you simply do pickle.loadyou should be reading the first object serialized into the file (not the last one as you've written).

如果您只是这样做,pickle.load您应该读取序列化到文件中的第一个对象(而不是您编写的最后一个对象)。

After unserializing the first object, the file-pointer is at the beggining of the next object - if you simply call pickle.loadagain, it will read that next object - do that until the end of the file.

在反序列化第一个对象后,文件指针位于下一个对象的开始处——如果你pickle.load再次调用,它将读取下一个对象——这样做直到文件结束。

objects = []
with (open("myfile", "rb")) as openfile:
    while True:
        try:
            objects.append(pickle.load(openfile))
        except EOFError:
            break

回答by Eric MacLeod

You are not doing anything with it, you are only loading the file.

你没有对它做任何事情,你只是在加载文件。

for line in x:
    print x

will print each line. (In the second with the statement)

将打印每一行。(在第二个声明中)

回答by Noctis Skytower

The following is an example of how you might write and read a pickle file. Note that if you keep appending pickle data to the file, you will need to continue reading from the file until you find what you want or an exception is generated by reaching the end of the file. That is what the last function does.

以下是您如何编写和读取 pickle 文件的示例。请注意,如果您继续将pickle 数据附加到文件中,您将需要继续从文件中读取,直到找到所需内容或到达文件末尾时生成异常。这就是最后一个函数的作用。

import os
import pickle


PICKLE_FILE = 'pickle.dat'


def main():
    # append data to the pickle file
    add_to_pickle(PICKLE_FILE, 123)
    add_to_pickle(PICKLE_FILE, 'Hello')
    add_to_pickle(PICKLE_FILE, None)
    add_to_pickle(PICKLE_FILE, b'World')
    add_to_pickle(PICKLE_FILE, 456.789)
    # load & show all stored objects
    for item in read_from_pickle(PICKLE_FILE):
        print(repr(item))
    os.remove(PICKLE_FILE)


def add_to_pickle(path, item):
    with open(path, 'ab') as file:
        pickle.dump(item, file, pickle.HIGHEST_PROTOCOL)


def read_from_pickle(path):
    with open(path, 'rb') as file:
        try:
            while True:
                yield pickle.load(file)
        except EOFError:
            pass


if __name__ == '__main__':
    main()

回答by Taylrl

There is a read_picklefunction as part of pandas 0.22+

有一个read_pickle函数作为 pandas 0.22+ 的一部分

import pandas as pd

object = pd.read_pickle(r'filepath')

回答by Christo

I developed a software tool that opens (most) Pickle files directly in your browser (nothing is transferred so it's 100% private):

我开发了一个软件工具,可以直接在您的浏览器中打开(大多数)Pickle 文件(没有传输任何内容,因此它是 100% 私密的):

https://pickleviewer.com/

https://pickleviewer.com/