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

Store multiple dictionaries in a file

P: n/a
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

Jul 19 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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 'o****@xiludom.gro'.split('@')])"
Jul 19 '05 #2

P: n/a

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.

Jul 19 '05 #3

P: n/a
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/
Jul 19 '05 #4

P: n/a
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?
Jul 19 '05 #5

P: n/a
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?
Jul 19 '05 #6

P: n/a
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

Jul 19 '05 #7

P: n/a

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

Jul 19 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.