如何首先读取二进制 pickle 数据,然后取消它?

发布于 2024-08-31 22:30:39 字数 594 浏览 7 评论 0原文

我正在解封磁盘上大小约为 1GB 的 NetworkX 对象。虽然我将它保存为二进制格式(使用协议 2),但解封这个文件需要很长时间——至少半个小时。我运行的系统有足够的系统内存(128 GB),所以这不是瓶颈。

我在此处读到,首先将整个文件读入内存,然后再读取,可以加快酸洗速度unpickle 它(该特定线程指的是 python 3.0,我没有使用它,但这一点在 python 2.6 中仍然应该是正确的)。

如何首先读取二进制文件,然后取消它?我尝试过:

import cPickle as pickle
f = open("big_networkx_graph.pickle","rb")
bin_data = f.read()
graph_data = pickle.load(bin_data)

但这返回:

TypeError: argument must have 'read' and 'readline' attributes

有什么想法吗?

I'm unpickling a NetworkX object that's about 1GB in size on disk. Although I saved it in the binary format (using protocol 2), it is taking a very long time to unpickle this file---at least half an hour. The system I'm running on has plenty of system memory (128 GB), so that's not the bottleneck.

I've read here that pickling can be sped up by first reading the entire file into memory, and then unpickling it (that particular thread refers to python 3.0, which I'm not using, but the point should still be true in python 2.6).

How do I first read the binary file, and then unpickle it? I have tried:

import cPickle as pickle
f = open("big_networkx_graph.pickle","rb")
bin_data = f.read()
graph_data = pickle.load(bin_data)

But this returns:

TypeError: argument must have 'read' and 'readline' attributes

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

请帮我爱他 2024-09-07 22:30:39

pickle.load(file) 需要一个类似文件的对象。相反,请使用:

pickle.loads(string)

从字符串中读取 pickled 对象层次结构。字符串中超出腌制对象表示的字符将被忽略。

pickle.load(file) expects a file-like object. Instead, use:

pickle.loads(string)

Read a pickled object hierarchy from a string. Characters in the string past the pickled object’s representation are ignored.

落墨 2024-09-07 22:30:39

该文档提到 StringIO ,我认为这是一种可能的解决方案。

尝试:

f = open("big_networkx_graph.pickle","rb")
bin_data = f.read()
sio = StringIO(bin_data)
graph_data = pickle.load(sio)

The documentation mentions StringIO, which I think is one possible solution.

Try:

f = open("big_networkx_graph.pickle","rb")
bin_data = f.read()
sio = StringIO(bin_data)
graph_data = pickle.load(sio)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文