1

我有大型(~75MB)腌制对象,可在映射的网络驱动器上使用(例如:X:/folder1/large_pickled_item.pk) 对象包含 numpy 数组+python 列表,并使用 cPickle 进行腌制,协议 2

当我尝试 unpickle 数据时,我收到以下错误消息:

使用pickle:KeyError:(随机字符)

使用 cPickle:IOError:[Errno 22] 无效参数

如果腌制的对象较小,或者将(较大的)对象复制到本地驱动器并运行相同的脚本,则不会出现错误。

知道问题出在哪里吗?是 python+pickle 问题还是 Windows 共享问题?

笔记:

  1. 我在 Windows XP Professional (SP3) 上使用 Python 2.7.2
  2. 我无法控制对象格式,我不创建它们,我只能读取它们
  3. 示例堆栈跟踪:

    文件“test.py”,第 38 行,在 getObject obj = pickle.load(input) 文件“C:\software\python\lib\pickle.py”,第 1378 行,在加载中返回 Unpickler(file).load()文件“C:\software\python\lib\pickle.py”,第 858 行,在 load dispatchkey KeyError: '~'

解决方案

  1. 以 67076095 字节的块将文件读入字符串缓冲区。
  2. 使用字符串缓冲区调用 pickle.loads 而不是使用文件对象调用 pickle.load
4

1 回答 1

1

This is due to a Windows bug, whereby reading and writing network files in chunks larger than 64MB does not work.

I suggest trying the mirror image of the workaround presented in https://stackoverflow.com/a/4228291/367273

If that doesn't help, perhaps you could create a wrapper for the file object that would automatically split every large read() into multiple smaller reads, and present that wrapper to the pickle module?

于 2012-05-02T11:01:54.773 回答