开发者

Efficient way to store dictionary (hash) in file with python?

I'm implementing a Unix userland tool that needs to store a hash on the disk. The hash will be read every run of the program, pretty frequently. The hash needs to store "name:path" values only.

I looked at the bsddb standard library module for python, but I can see it will be deprecated in Python 3. I also saw the pickle standard library module.

I'm not a python guy, so what is the efficient way for 开发者_运维知识库hash serialization and frequent open/read/close operations?


I would start with the shelve module and see if that isn't too slow. It does exactly what you want.

import shelve

d = shelve.open('filename')

d['name'] = 'path'

d.close()

or to read from it

d = shelve.open('filename')

d = hash['name']

It's essentially a wrapper around pickle that provides a dictionary abstraction.


I'd use pickle and see if it's fast enough for your needs.


I would suggest you use pickle / shelve for serialization of data.

  • http://www.saltycrane.com/blog/2008/01/saving-python-dict-to-file-using-pickle/
  • http://docs.python.org/library/shelve.html
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜