save a dictionary in a file

L

Luis P. Mendes

-----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-----
 
E

Eric Davis

Luis said:
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
 
M

Mariano Draghi

Luis said:
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,
 
D

Dan Perl

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
 
J

Jeff Shannon

Luis said:
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
 
J

Josiah Carlson

Jeff Shannon said:
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
 
J

Jeff Shannon

Josiah said:
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
 
J

Josiah Carlson

Jeff Shannon said:
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
 
S

Stelios Xanthakis

Luis said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top