Pickle与搁置在Python中存储大型词典

20 python shelve pickle

如果我将一个大目录存储为一个pickle文件,是否通过cPickle意味着它将全部被一次性地存入内存来加载它?

如果是这样,是否有一种跨平台的方式来获得类似的东西pickle,但是在一个项目上访问每个条目一个键(即避免将所有字典加载到内存中,只按名称加载每个条目)?我知道shelve应该这样做:那就像便携式一样pickle吗?

jim*_*ark 21

我知道shelve应该这样做:虽然像pickle一样便携吗?

是.shelvePython标准库的一部分,用Python编写.

编辑

所以,如果你有一个大词典:

bigd = {'a': 1, 'b':2, # . . .
}
Run Code Online (Sandbox Code Playgroud)

并且你想保存它而不必在以后阅读整个事情,然后不要将它保存为泡菜,最好将其保存为一个架子,一种磁盘字典.

import shelve

myShelve = shelve.open('my.shelve')
myShelve.update(bigd)
myShelve.close()
Run Code Online (Sandbox Code Playgroud)

然后你可以:

import shelve

myShelve = shelve.open('my.shelve')
value = myShelve['a']
value += 1
myShelve['a'] = value
Run Code Online (Sandbox Code Playgroud)

您基本上将搁置对象视为dict,但这些项目存储在磁盘上(作为单独的pickle)并根据需要读入.

如果您的对象可以存储为属性列表,那么sqlite可能是一个不错的选择.货架和泡菜很方便,但只能通过Python访问,但sqlite数据库可以从大多数语言中读取.

  • 搁置不跨越平台 (3认同)

Mik*_*rns 6

如果你想要一个比它更强大的模块shelve,你可能会看到klepto. klepto构建用于为磁盘或数据库上与平台无关的存储提供字典接口,并且可以处理大型数据.

在这里,我们首先创建一些存储在磁盘上的pickle对象.他们使用the dir_archive,每个文件存储一个对象.

>>> d = dict(zip('abcde',range(5)))
>>> d['f'] = max
>>> d['g'] = lambda x:x**2
>>> 
>>> import klepto
>>> help(klepto.archives.dir_archive)       

>>> print klepto.archives.dir_archive.__new__.__doc__
initialize a dictionary with a file-folder archive backend

    Inputs:
        name: name of the root archive directory [default: memo]
        dict: initial dictionary to seed the archive
        cached: if True, use an in-memory cache interface to the archive
        serialized: if True, pickle file contents; otherwise save python objects
        compression: compression level (0 to 9) [default: 0 (no compression)]
        memmode: access mode for files, one of {None, 'r+', 'r', 'w+', 'c'}
        memsize: approximate size (in MB) of cache for in-memory compression

>>> a = klepto.archives.dir_archive(dict=d)
>>> a
dir_archive('memo', {'a': 0, 'c': 2, 'b': 1, 'e': 4, 'd': 3, 'g': <function <lambda> at 0x102f562a8>, 'f': <built-in function max>}, cached=True)
>>> a.dump()
>>> del a
Run Code Online (Sandbox Code Playgroud)

现在,数据全部在磁盘上,让我们选择我们要加载到内存中的数据.b是内存中的dict,同时b.archive将文件集合映射到字典视图中.

>>> b = klepto.archives.dir_archive('memo')
>>> b
dir_archive('memo', {}, cached=True)
>>> b.keys()   
[]
>>> b.archive.keys()
['a', 'c', 'b', 'e', 'd', 'g', 'f']
>>> b.load('a')
>>> b
dir_archive('memo', {'a': 0}, cached=True)
>>> b.load('b')
>>> b.load('f')
>>> b.load('g')
>>> b['g'](b['f'](b['a'],b['b']))
1
Run Code Online (Sandbox Code Playgroud)

klepto还为sql存档提供相同的界面.

>>> print klepto.archives.sql_archive.__new__.__doc__
initialize a dictionary with a sql database archive backend

    Connect to an existing database, or initialize a new database, at the
    selected database url. For example, to use a sqlite database 'foo.db'
    in the current directory, database='sqlite:///foo.db'. To use a mysql
    database 'foo' on localhost, database='mysql://user:pass@localhost/foo'.
    For postgresql, use database='postgresql://user:pass@localhost/foo'. 
    When connecting to sqlite, the default database is ':memory:'; otherwise,
    the default database is 'defaultdb'. If sqlalchemy is not installed,
    storable values are limited to strings, integers, floats, and other
    basic objects. If sqlalchemy is installed, additional keyword options
    can provide database configuration, such as connection pooling.
    To use a mysql or postgresql database, sqlalchemy must be installed.

    Inputs:
        name: url for the sql database [default: (see note above)]
        dict: initial dictionary to seed the archive
        cached: if True, use an in-memory cache interface to the archive
        serialized: if True, pickle table contents; otherwise cast as strings

>>> c = klepto.archives.sql_archive('database')
>>> c.update(b)
>>> c
sql_archive('sqlite:///database', {'a': 0, 'b': 1, 'g': <function <lambda> at 0x10446b1b8>, 'f': <built-in function max>}, cached=True)
>>> c.dump()
Run Code Online (Sandbox Code Playgroud)

现在,磁盘上的相同对象也在sql存档中.我们可以将新对象添加到存档中.

>>> b['x'] = 69
>>> c['y'] = 96
>>> b.dump('x')
>>> c.dump('y')
Run Code Online (Sandbox Code Playgroud)

获取klepto此:https://github.com/uqfoundation

  • 请注意,我是 `klepto` 的作者 (2认同)