By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,732 Members | 1,461 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,732 IT Pros & Developers. It's quick & easy.

save a dictionary in a file

P: n/a
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi,

my program builds a dictionary that I would like to save in a file.

My question is what are the simple ways to do it?

The first solution I've thought of is to transform the dictionary in a
list, with the key being the first element on that list, and the value
as the second. Then I could use pickle to do the write and read
operations. It seems simple and easy.

Is there a more commonly used form to do it, yet in a simple way?

Luis
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFBmPRdHn4UHCY8rB8RApRjAKCF4/pEMbQQ10Quf5CZO/nTL2CpHgCfVWZF
G36HuPUbLXC1qtB+DP8BhK0=
=f+vS
-----END PGP SIGNATURE-----
Jul 18 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Luis P. Mendes wrote:
Hi,

my program builds a dictionary that I would like to save in a file.

My question is what are the simple ways to do it?

The first solution I've thought of is to transform the dictionary in a
list, with the key being the first element on that list, and the value
as the second. Then I could use pickle to do the write and read
operations. It seems simple and easy.

Is there a more commonly used form to do it, yet in a simple way?

Luis


Look at the pickle or ConfigParser modules.
Both of these will do what you are looking for in different ways.

Eric

Jul 18 '05 #2

P: n/a
Luis P. Mendes wrote:
Hash: SHA1

Hi,

my program builds a dictionary that I would like to save in a file.

My question is what are the simple ways to do it?


We implemented this for SiGeFi (sf.net/projects/sigefi/)

From config.py

class PersistentDict(dict):
'''Persistent dictionary.

It's a dictionary that saves and loads its data to a file.
'''

def __init__(self, nomarchivo):
'''Creates a PersistentDict instance.

Receives a filename, but if the file does not exist (yet)
starts to work
with an empty data set and will create the file when the data
is saved.

:Parameters:
- `nomarchivo`: file where the data is saved.
'''
self._nomarchivo = nomarchivo
dict.__init__(self)

if not os.access(nomarchivo, os.W_OK):
return

arch = open(nomarchivo, 'r')
olddict = cPickle.load(arch)
arch.close()
self.update(olddict)
return

def save(self):
'''Saves the data to disk.'''
arch = open(self._nomarchivo, 'w')
cPickle.dump(self, arch)
arch.close()
return

Regards,

--
Mariano

Jul 18 '05 #3

P: n/a
Maybe I am missing something, but why don't you just pickle the dictionary
itself and instead you transform it into a list and pickle the list? I
don't know of any restrictions in pickling a dictionary and I do that in
some of my code. I actually pickle a structure of nested dictionaries (ie,
values in the top dictionary are dictionaries themselves, some of their
values are dictionaries, and so on).

Dan

"Luis P. Mendes" <lu************@netvisaoXX.pt> wrote in message
news:2v*************@uni-berlin.de...
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi,

my program builds a dictionary that I would like to save in a file.

My question is what are the simple ways to do it?

The first solution I've thought of is to transform the dictionary in a
list, with the key being the first element on that list, and the value
as the second. Then I could use pickle to do the write and read
operations. It seems simple and easy.

Is there a more commonly used form to do it, yet in a simple way?

Luis
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFBmPRdHn4UHCY8rB8RApRjAKCF4/pEMbQQ10Quf5CZO/nTL2CpHgCfVWZF
G36HuPUbLXC1qtB+DP8BhK0=
=f+vS
-----END PGP SIGNATURE-----

Jul 18 '05 #4

P: n/a
Luis P. Mendes wrote:

Is there a more commonly used form to do it, yet in a simple way?


Yes. If the dictionary contains only strings, numbers, lists and tuples
of strings, numbers, and lists and tuples, of (infinite resursion.
program halted)

You can

FILE.write ('D=' + str (D))

and then execfile the file.

Stelios
Jul 18 '05 #5

P: n/a
Luis P. Mendes wrote:

my program builds a dictionary that I would like to save in a file.

As others have said, there should be no reason to convert to a list
before pickling -- AFAIK, there's no ban against pickling dictionaries.

You might also look into the Shelve module, which implements a
filesystem-persisting object with dictionary access syntax. (And which,
IIRC, uses pickled dictionaries under the covers.)

Jeff Shannon
Technician/Programmer
Credit International

Jul 18 '05 #6

P: n/a

Jeff Shannon <je**@ccvcorp.com> wrote:

Luis P. Mendes wrote:

my program builds a dictionary that I would like to save in a file.

As others have said, there should be no reason to convert to a list
before pickling -- AFAIK, there's no ban against pickling dictionaries.

You might also look into the Shelve module, which implements a
filesystem-persisting object with dictionary access syntax. (And which,
IIRC, uses pickled dictionaries under the covers.)


It doesn't. It uses a bsddb database.

A bsddb (really anydbm which can give you bsddb) database is merely a
string key->string value mapping (for btree and hash mappings). You
access it via string keys, and its values are automatically translated
to/from strings via Pickle. The only way you actually pickle and
unpickle dictionaries is by having a dictionary in a value.
- Josiah

Jul 18 '05 #7

P: n/a
Josiah Carlson wrote:
Jeff Shannon <je**@ccvcorp.com> wrote:

Luis P. Mendes wrote:
my program builds a dictionary that I would like to save in a file.

As others have said, there should be no reason to convert to a list
before pickling -- AFAIK, there's no ban against pickling dictionaries.

You might also look into the Shelve module, which implements a
filesystem-persisting object with dictionary access syntax. (And which,
IIRC, uses pickled dictionaries under the covers.)


It doesn't. It uses a bsddb database.

A bsddb (really anydbm which can give you bsddb) database is merely a
string key->string value mapping (for btree and hash mappings). You
access it via string keys, and its values are automatically translated
to/from strings via Pickle. The only way you actually pickle and
unpickle dictionaries is by having a dictionary in a value.


Ah, my mistake. (I've never really had call to use shelve *or* pickle,
and was going off of (apparently erroneous) memories of conversations in
c.l.py.)

Still, it does seem that shelve would probably fit the O.P.'s needs...

Jeff Shannon
Technician/Programmer
Credit International

Jul 18 '05 #9

P: n/a

Jeff Shannon <je**@ccvcorp.com> wrote:

Josiah Carlson wrote:
Jeff Shannon <je**@ccvcorp.com> wrote:

Luis P. Mendes wrote:

my program builds a dictionary that I would like to save in a file.
As others have said, there should be no reason to convert to a list
before pickling -- AFAIK, there's no ban against pickling dictionaries.

You might also look into the Shelve module, which implements a
filesystem-persisting object with dictionary access syntax. (And which,
IIRC, uses pickled dictionaries under the covers.)


It doesn't. It uses a bsddb database.

A bsddb (really anydbm which can give you bsddb) database is merely a
string key->string value mapping (for btree and hash mappings). You
access it via string keys, and its values are automatically translated
to/from strings via Pickle. The only way you actually pickle and
unpickle dictionaries is by having a dictionary in a value.


Ah, my mistake. (I've never really had call to use shelve *or* pickle,
and was going off of (apparently erroneous) memories of conversations in
c.l.py.)

Still, it does seem that shelve would probably fit the O.P.'s needs...


It depends on how much reading and writing to/from the dictionary is
done. For many reads, a caching shelve instance works well, but ends up
creating an in-memory copy anyways. When writing, one needs to be
careful of the number of writes and how long it takes to update the
shelve instance, and whether one wants a bunch of writes to the instance
done during close.

- Josiah

Jul 18 '05 #10

P: n/a
Mariano Draghi wrote:
class PersistentDict(dict):
'''Persistent dictionary.

It's a dictionary that saves and loads its data to a file.
'''


Sounds pretty similar to shelve
(http://python.org/doc/lib/module-shelve.html) to me. Have you
considered using it instead?
Jul 18 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.