Python 是什么导致错误“_pickle.UnpicklingError: invalid load key, ' '.”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33049688/
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 12:40:19  来源:igfitidea点击:

What causes the error "_pickle.UnpicklingError: invalid load key, ' '."?

pythonpickle

提问by Xcecution

I'm trying to storage 5000 data elements on an array. This 5000 elements are storage on an existent file (therefore it's not empty).

我正在尝试在一个数组上存储 5000 个数据元素。这 5000 个元素存储在现有文件中(因此它不是空的)。

But I'm getting an error and I don't know what is causing it.

但是我收到一个错误,我不知道是什么原因造成的。

IN:

在:

def array():

    name = 'puntos.df4'

    m = open(name, 'rb')
    v = []*5000

    m.seek(-5000, io.SEEK_END)
    fp = m.tell()
    sz = os.path.getsize(name)

    while fp < sz:
        pt = pickle.load(m)
        v.append(pt)

    m.close()
    return v

OUT:

出去:

line 23, in array
pt = pickle.load(m)
_pickle.UnpicklingError: invalid load key, ''.

采纳答案by Mike McKerns

pickling is recursive, not sequential. Thus, to pickle a list, picklewill start to pickle the containing list, then pickle the first element… diving into the first element and pickling dependencies and sub-elements until the first element is serialized. Then moves on to the next element of the list, and so on, until it finally finishes the list and finishes serializing the enclosing list. In short, it's hard to treat a recursive pickle as sequential, except for some special cases. It's better to use a smarter pattern on your dump, if you want to loadin a special way.

酸洗是递归的,而不是顺序的。因此,要腌制一个列表,pickle将开始腌制包含列表,然后腌制第一个元素……深入研究第一个元素并腌制依赖项和子元素,直到第一个元素被序列化。然后移动到列表的下一个元素,依此类推,直到它最终完成列表并完成对封闭列表的序列化。简而言之,除非某些特殊情况,否则很难将递归泡菜视为连续的。dump如果您想以load特殊方式使用更智能的模式,则更好。

The most common pickle, it to pickle everything with a single dumpto a file -- but then you have to loadeverything at once with a single load. However, if you open a file handle and do multiple dumpcalls (e.g. one for each element of the list, or a tuple of selected elements), then your loadwill mirror that… you open the file handle and do multiple loadcalls until you have all the list elements and can reconstruct the list. It's still not easy to selectively loadonly certain list elements, however. To do that, you'd probably have to store your list elements as a dict(with the index of the element or chunk as the key) using a package like klepto, which can break up a pickled dictinto several files transparently, and enables easy loading of specific elements.

最常见的pickle,它用一个dump文件将load所有内容腌制到一个文件中——但是你必须用一个load. 但是,如果您打开一个文件句柄并进行多次dump调用(例如,针对列表中的每个元素或一组选定元素进行一次调用),那么您load将镜像……您打开文件句柄并进行多次load调用,直到您拥有所有列表元素并可以重建列表。load然而,仅选择性地选择某些列表元素仍然不容易。为此,您可能必须dict使用类似 的包将列表元素存储为 a (以元素或块的索引作为键)klepto,这可以分解腌制dict透明地放入多个文件中,并可以轻松加载特定元素。

Saving and loading multiple objects in pickle file?

在pickle文件中保存和加载多个对象?

回答by yurib

I am not completely sure what you're trying to achieve by seeking to a specific offset and attempting to load individual values manually, the typical usage of the picklemodule is:

我不完全确定您要通过寻找特定偏移量并尝试手动加载单个值来实现什么,该pickle模块的典型用法是:

# save data to a file
with open('myfile.pickle','wb') as fout:
    pickle.dump([1,2,3],fout)

# read data from a file
with open('myfile.pickle') as fin:
    print pickle.load(fin)

# output
>> [1, 2, 3]

If you dumped a list, you'll load a list, there's no need to load each item individually.

如果您转储一个列表,您将加载一个列表,无需单独加载每个项目。

you're saying that you got an error before you were seeking to the -5000 offset, maybe the file you're trying to read is corrupted.

您是说在寻找 -5000 偏移量之前遇到错误,可能是您尝试读取的文件已损坏。

If you have access to the original data, I suggest you try saving it to a new file and reading it as in the example.

如果您可以访问原始数据,我建议您尝试将其保存到一个新文件并按照示例中的方式读取它。

回答by mishaF

This may not be relevant to your specific issue, but I had a similar problem when the pickle archive had been created using gzip.

这可能与您的具体问题无关,但是当使用gzip.

For example if a compressed pickle archive is made like this,

例如,如果压缩的泡菜存档是这样制作的,

import gzip, pickle
with gzip.open('test.pklz', 'wb') as ofp:
    pickle.dump([1,2,3], ofp)

Trying to open it throws the errors

尝试打开它会引发错误

 with open('test.pklz', 'rb') as ifp:
     print(pickle.load(ifp))
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
_pickle.UnpicklingError: invalid load key, ''.

But, if the pickle file is opened using gzipall is harmonious

但是,如果使用gzipall打开pickle文件是和谐的

with gzip.open('test.pklz', 'rb') as ifp:
    print(pickle.load(ifp))

[1, 2, 3]

回答by foladev

If you transferred these files through disk or other means, it is likely they were not saved properly.

如果您通过磁盘或其他方式传输这些文件,很可能它们没有正确保存。