Help on dictionaries...
Souvik Dutta
souvik.viksou at gmail.com
Thu Jan 30 01:56:23 EST 2020
How do I connect it with my dictionary
On Thu, Jan 30, 2020, 7:03 AM Tim Chase <python.list at tim.thechases.com>
wrote:
> On 2020-01-30 06:44, Souvik Dutta wrote:
> > Hey I was thinking how I can save a dictionary in python(obviously)
> > so that the script is rerun it automatically loads the dictionary.
>
> This is almost exactly what the "dbm" (nee "anydbm") module does, but
> persisting the dictionary out to the disk:
>
> import dbm
> from sys import argv
> with dbm.open("my_cache", "c") as db:
> if len(argv) > 1:
> key = argv[1]
> if key in db:
> print("Found it:", db[key])
> else:
> print("Not found. Adding")
> if len(argv) > 2:
> value = argv[2]
> else:
> value = key
> db[key] = value
> else:
> print("There are %i items in the cache" % len(db))
>
> The resulting "db" acts like a dictionary, but persists.
>
> If you really must have the results as a "real" dict, you can do the
> conversion:
>
> real_dict = dict(db)
>
> -tkc
>
>
>
>
More information about the Python-list
mailing list