如何cPickle转储并将单独的词典加载到同一文件? [英] How to cPickle dump and load separate dictionaries to the same file?

查看:51
本文介绍了如何cPickle转储并将单独的词典加载到同一文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个运行并创建三个字典的过程:2个较小的字典和1个较大的字典.

I have a process which runs and creates three dictionaries: 2 rather small, and 1 large.

我知道我可以像这样存储一本字典

I know I can store one dictionary like:

import cPickle as pickle
with open(filename, 'wb') as fp:
  pickle.dump(self.fitResults, fp)

我想做的是将所有3个字典存储在同一个文件中,并能够在另一时间分别加载这3个字典.像

What I'd like to do is store all 3 dictionaries in the same file, with the ability to load in the three dictionaries separately at another time. Something like

with open(filename, 'rb') as fp:
  dict1, dict2, dict3 = pickle.load(fp)

或者更好的方法是只加载前两个字典,并选择是否加载第三个(大)字典.这有可能还是我应该以一种完全不同的方式来解决这个问题?

Or even better just load the first two dictionaries, and make it optional whether to load the third (large) one. Is this possible or should I go about this in a completely different way?

推荐答案

当然,您只需要分别转储每个文件,然后分别加载它们即可:

Sure, you just dump each one separately and then load them separately:

with open(filename,'wb') as fp:
    pickle.dump(dict1,fp)
    pickle.dump(dict2,fp)
    pickle.dump(dict3,fp)

with open(filename,'rb') as fp:
    d1=pickle.load(fp)
    d2=pickle.load(fp)
    d3=pickle.load(fp)

请确保将最后一个大文件丢弃,这样您就可以加载小文件而不必先加载大文件.我想您甚至可以聪明地将每个转储开始的文件位置存储在各种标头中,然后可以在加载之前搜索到该位置(但是开始变得有些复杂).

make sure to dump the big on last so you can load the little ones without loading the big one first. I imagine you could even get clever and store the file positions where each dump starts in a header of sorts and then you could seek to that location before loading (but that's starting to get a little more complicated).

这篇关于如何cPickle转储并将单独的词典加载到同一文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆