Store multiple dictionaries in a file

Discussion in 'Python' started by Philipp H. Mohr, Jun 30, 2005.

  1. 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
     
    Philipp H. Mohr, Jun 30, 2005
    #1
    1. Advertising

  2. 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 ''.split('@')])"
     
    bruno modulix, Jun 30, 2005
    #2
    1. Advertising

  3. 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.
     
    Philipp H. Mohr, Jun 30, 2005
    #3
  4. 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/
     
    Jeremy Sanders, Jun 30, 2005
    #4
  5. Philipp H. Mohr

    John Machin Guest

    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?
     
    John Machin, Jun 30, 2005
    #5
  6. Philipp H. Mohr

    John Machin Guest

    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?
     
    John Machin, Jun 30, 2005
    #6
  7. Philipp H. Mohr

    Larry Bates Guest

    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
    >
     
    Larry Bates, Jun 30, 2005
    #7
  8. 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
     
    Philipp H. Mohr, Jun 30, 2005
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?UnVkeQ==?=

    to store or not to store an image

    =?Utf-8?B?UnVkeQ==?=, Mar 29, 2005, in forum: ASP .Net
    Replies:
    6
    Views:
    645
    =?Utf-8?B?UnVkeQ==?=
    Mar 30, 2005
  2. Livin
    Replies:
    5
    Views:
    323
    Ben Sizer
    Jan 18, 2006
  3. manstey

    pickling multiple dictionaries

    manstey, May 24, 2006, in forum: Python
    Replies:
    3
    Views:
    264
    manstey
    May 25, 2006
  4. lysdexia
    Replies:
    6
    Views:
    505
    John Machin
    Dec 2, 2007
  5. Brandon
    Replies:
    12
    Views:
    491
    Brandon
    Aug 15, 2008
Loading...

Share This Page