With Python, can I keep a persistent dictionary and modify it?

后端 未结 7 1277
孤城傲影
孤城傲影 2020-12-16 16:23

So, I want to store a dictionary in a persistent file. Is there a way to use regular dictionary methods to add, print, or delete entries from the dictionary in that file?

相关标签:
7条回答
  • 2020-12-16 16:57

    Use JSON

    Similar to Pete's answer, I like using JSON because it maps very well to python data structures and is very readable:

    Persisting data is trivial:

    >>> import json
    >>> db = {'hello': 123, 'foo': [1,2,3,4,5,6], 'bar': {'a': 0, 'b':9}}
    >>> fh = open("db.json", 'w')
    >>> json.dump(db, fh)
    

    and loading it is about the same:

    >>> import json
    >>> fh = open("db.json", 'r')
    >>> db = json.load(fh)
    >>> db
    {'hello': 123, 'bar': {'a': 0, 'b': 9}, 'foo': [1, 2, 3, 4, 5, 6]}
    >>> del new_db['foo'][3]
    >>> new_db['foo']
    [1, 2, 3, 5, 6]
    

    In addition, JSON loading doesn't suffer from the same security issues that shelve and pickle do, although IIRC it is slower than pickle.

    If you want to write on every operation:

    If you want to save on every operation, you can subclass the Python dict object:

    import os
    import json
    
    class DictPersistJSON(dict):
        def __init__(self, filename, *args, **kwargs):
            self.filename = filename
            self._load();
            self.update(*args, **kwargs)
    
        def _load(self):
            if os.path.isfile(self.filename) 
               and os.path.getsize(self.filename) > 0:
                with open(self.filename, 'r') as fh:
                    self.update(json.load(fh))
    
        def _dump(self):
            with open(self.filename, 'w') as fh:
                json.dump(self, fh)
    
        def __getitem__(self, key):
            return dict.__getitem__(self, key)
    
        def __setitem__(self, key, val):
            dict.__setitem__(self, key, val)
            self._dump()
    
        def __repr__(self):
            dictrepr = dict.__repr__(self)
            return '%s(%s)' % (type(self).__name__, dictrepr)
    
        def update(self, *args, **kwargs):
            for k, v in dict(*args, **kwargs).items():
                self[k] = v
            self._dump()
    

    Which you can use like this:

    db = DictPersistJSON("db.json")
    db["foo"] = "bar" # Will trigger a write
    

    Which is woefully inefficient, but can get you off the ground quickly.

    0 讨论(0)
提交回复
热议问题