473,324 Members | 1,856 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

save a dictionary in a file

-----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
10 6934
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
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
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
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
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

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

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Thomas Philips | last post by:
I want to print "1 spam 4 you" using a formatted string that gets its inputs from the dictionary d={'n1':1, 's1':'spam', 'n2':4}. To do so, I write >>> x="%('n1')d %('s1')s %('n2')d you" >>> x...
12
by: Antoon Pardon | last post by:
Well at least I find them missing. For the moment I frequently come across the following cases. 1) Two files, each with key-value pairs for the same dictionary. However it is an error if the...
7
by: rickle | last post by:
I'm trying to compare sun patch levels on a server to those of what sun is recommending. For those that aren't familiar with sun patch numbering here is a quick run down. A patch number shows...
1
by: Philip Chan | last post by:
I am using ASP3.0 and IIS5 I want to have OOP so I have define a class call Employee with property 'fullName' and methods like getFullName(). To save a group of employee in session, I use a...
0
by: ssg31415926 | last post by:
I've been trying to save a hashtable in an Application Settings file. I need to save settings for each tabPage on a form. Trouble is, the number of tabPages is determined at runtime, so I can't...
9
by: oz | last post by:
Hi All, I want to make a dictionary with windows application. My data is html format. therefore, i use webBrowser control on my windows form. If the user click any word in webBrowser control,...
9
by: nik | last post by:
Hi, I would like to create a class and then save it for re-use later. I have tried to use pickle, but am not sure if that is right. I am sorry, but I am new to python. Basically, I have a...
6
by: globalrev | last post by:
i extract info from one file and put it into a dictionary. i want to save that dictionary for later use, how do i do that? might save a list of dictionaries or a list of classobjects too if there...
20
by: Simon Strobl | last post by:
Hello, I tried to load a 6.8G large dictionary on a server that has 128G of memory. I got a memory error. I used Python 2.5.2. How can I load my data? SImon
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.