Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Store multiple dictionaries in a file

Reply
Thread Tools

Store multiple dictionaries in a file

 
 
Philipp H. Mohr
Guest
Posts: n/a
 
      06-30-2005
Hello,

I would like to store multiple dictionaries in a file, if possible one per
line. My code currently produces a new dictionary every iteration and
passes it on to another peace of code. In order to be able to re-run some
experiments at a later date I would like to store every dictionary in the
same file.
I looked at pickel, but that seems to require a whole file for each
dictionary.

It would be great if some one could tell me how to do that.

Thank you,
Phil

 
Reply With Quote
 
 
 
 
bruno modulix
Guest
Posts: n/a
 
      06-30-2005
Philipp H. Mohr wrote:
> Hello,
>
> I would like to store multiple dictionaries in a file, if possible one per
> line.


Why "one per line" ?

> My code currently produces a new dictionary every iteration and
> passes it on to another peace of code.


May this code rest in piece <grin>

> In order to be able to re-run some
> experiments at a later date I would like to store every dictionary in the
> same file.
> I looked at pickel, but that seems to require a whole file for each
> dictionary.
>
> It would be great if some one could tell me how to do that.


A pretty simple solution could be to store all the dicts in another
container (list or dict, depending on how you need to retrieve'em, but
from what you explain I'd say a list) and then pickle this container.

My 2 cents...
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in '(E-Mail Removed)'.split('@')])"
 
Reply With Quote
 
 
 
 
Philipp H. Mohr
Guest
Posts: n/a
 
      06-30-2005

Thank you for you answer.

> > I would like to store multiple dictionaries in a file, if possible one per
> > line.

>
> Why "one per line" ?


I agree with you that it sounds like nasty code but there is a good
reason for doing it this way - I think. My code collects data (attributes)
of its current environment, e.g. date, time, location, etc.
These values are put into a dictionary and passed to another program which
processes the data. The dictionary (or vector of attributes) is the only
interface between both progs. The one which creates the dictionary can
forget about it after it has passed it on. This is where the storing comes
into action. In order to be able to re-run an experiment I want to store
the dictionaries in a file. Also the program might not run continuasly,
therefore if I write all of them to a file, on after the other, I would be
able to re-run the experiment much easier.

Hope this makes sense.

Thank you,
Phil

>
> A pretty simple solution could be to store all the dicts in another
> container (list or dict, depending on how you need to retrieve'em, but
> from what you explain I'd say a list) and then pickle this container.

 
Reply With Quote
 
Jeremy Sanders
Guest
Posts: n/a
 
      06-30-2005
Philipp H. Mohr wrote:

> I would like to store multiple dictionaries in a file, if possible one per
> line. My code currently produces a new dictionary every iteration and
> passes it on to another peace of code. In order to be able to re-run some
> experiments at a later date I would like to store every dictionary in the
> same file.
> I looked at pickel, but that seems to require a whole file for each
> dictionary.


If you're not worried about security, you could write the repr() of each
dict to the file and get the values back by using the eval() function.
repr() writes onto one line.

If you're storing types without repr() representations this will not work.

Jeremy

--
Jeremy Sanders
http://www.jeremysanders.net/
 
Reply With Quote
 
John Machin
Guest
Posts: n/a
 
      06-30-2005
bruno modulix wrote:
> Philipp H. Mohr wrote:


>>My code currently produces a new dictionary every iteration and
>>passes it on to another peace of code.

>
>
> May this code rest in piece <grin>


Perhaps it's the piece of code that passeth all understanding?
 
Reply With Quote
 
John Machin
Guest
Posts: n/a
 
      06-30-2005
bruno modulix wrote:
> Philipp H. Mohr wrote:


>>My code currently produces a new dictionary every iteration and
>>passes it on to another peace of code.

>
>
> May this code rest in piece <grin>


Perhaps it's the piece of code which passeth all understanding?
 
Reply With Quote
 
Larry Bates
Guest
Posts: n/a
 
      06-30-2005
You might want to take a look at the shelve module.

-Larry

Philipp H. Mohr wrote:
> Hello,
>
> I would like to store multiple dictionaries in a file, if possible one per
> line. My code currently produces a new dictionary every iteration and
> passes it on to another peace of code. In order to be able to re-run some
> experiments at a later date I would like to store every dictionary in the
> same file.
> I looked at pickel, but that seems to require a whole file for each
> dictionary.
>
> It would be great if some one could tell me how to do that.
>
> Thank you,
> Phil
>

 
Reply With Quote
 
Philipp H. Mohr
Guest
Posts: n/a
 
      06-30-2005

Hello,

this is the solution I went for, as I am indeed not concernt about
security and the implementation is straight forward.

Thank you,
Phil


> If you're not worried about security, you could write the repr() of each
> dict to the file and get the values back by using the eval() function.
> repr() writes onto one line.
>
> If you're storing types without repr() representations this will not work.
>
> Jeremy

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
inheritance, multiple inheritance and the weaklist and instance dictionaries Rouslan Korneychuk Python 8 02-10-2011 04:02 AM
updating dictionaries from/to dictionaries Brandon Python 12 08-15-2008 12:35 AM
Pickling dictionaries containing dictionaries: failing,recursion-style! lysdexia Python 6 12-02-2007 12:03 AM
pickling multiple dictionaries manstey Python 3 05-24-2006 11:38 PM
search multiple dictionaries efficiently? Livin Python 5 01-18-2006 09:18 AM



Advertisments