Store multiple dictionaries in a file

P

Philipp H. Mohr

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
 
B

bruno modulix

Philipp said:
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 said:
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...
 
P

Philipp H. Mohr

Thank you for you answer.
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
 
J

Jeremy Sanders

Philipp said:
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
 
P

Philipp H. Mohr

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
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top