Store multiple dictionaries in a file

Andreas Kostyrka andreas at kostyrka.org
Thu Jun 30 13:24:21 CEST 2005


How so?

>>> import cPickle as cp
>>> f=open("/tmp/test.pck", "wb")
>>> cp.dump(dict(a=1), f)
>>> cp.dump(dict(b=1), f)
>>> f.close()
>>> f=open("/tmp/test.pck", "rb")
>>> cp.load(f)
{'a': 1}
>>> cp.load(f)
{'b': 1}
>>> cp.load(f)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
EOFError
>>> f.close()


On Thu, Jun 30, 2005 at 11:32:27AM +0100, 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
> 
> -- 
> http://mail.python.org/mailman/listinfo/python-list



More information about the Python-list mailing list