"pickle" vs. f.write()

Discussion in 'Python' started by Johan Kohler, Jan 26, 2005.

  1. Johan Kohler

    Johan Kohler Guest

    Hi,

    I have a class with attributes that are string, integer and list. eg.

    class person:
    name =""
    age = 0
    friends=[]
    comment=""""""

    me = person()

    I want to save a whole bunch of instances to a file, a classic "records"
    file I/O.

    To write the file, I can do f.write(str([me.name, me.age, me.friends,
    me.comment]) + "\n"

    This works nicely for writing, but when reading, I cannot convert the
    string easily to a list:
    list(f.readline()) is not the same as [me.name, me.age, me.friends,
    me.comment]

    I was wondering whether pickle might make this easier - an example would
    be much appreciated.

    Otherwise, what is the best "Python" way to write and read this data
    structure?

    Thanks in advance...

    Johan

    __
    Yes, I do feel stupid asking this, but time's-a-runnin' out..
    --
    Using Opera's revolutionary e-mail client: http://www.opera.com/m2/
    --------------------------------------------------------------------
    Please find our disclaimer at http://www.ukzn.ac.za/disclaimer
    --------------------------------------------------------------------
    <<<<gwavasig>>>>
    Johan Kohler, Jan 26, 2005
    #1
    1. Advertising

  2. Johan Kohler

    Peter Maas Guest

    Johan Kohler schrieb:
    > class person:
    > name =""
    > age = 0
    > friends=[]
    > comment=""""""
    >
    > me = person()
    >
    > Otherwise, what is the best "Python" way to write and read this data
    > structure?


    import pickle

    class person:
    name =""
    age = 0
    friends=[]
    comment=""""""

    me = person()

    # store
    pf = file('/tmp/pickletest', 'w')
    pickle.dump(me, pf)
    pf.close()


    # load
    pf = file('/tmp/pickletest', 'r')
    me2 = pickle.load(pf)
    pf.close()

    This is sequential access. If you want to have random access, look
    for shelve.

    --
    -------------------------------------------------------------------
    Peter Maas, M+R Infosysteme, D-52070 Aachen, Tel +49-241-93878-0
    E-mail 'cGV0ZXIubWFhc0BtcGx1c3IuZGU=\n'.decode('base64')
    -------------------------------------------------------------------
    Peter Maas, Jan 26, 2005
    #2
    1. Advertising

  3. Johan Kohler

    Johan Kohler Guest

    Thanks... works like a charm :)

    On Wed, 26 Jan 2005 12:55:38 +0100, Peter Maas <> wrote:

    > Johan Kohler schrieb:
    >> class person:
    >> name =""
    >> age = 0
    >> friends=[]
    >> comment=""""""
    >> me = person()
    >> Otherwise, what is the best "Python" way to write and read this data
    >> structure?

    >
    > import pickle
    >
    > class person:
    > name =""
    > age = 0
    > friends=[]
    > comment=""""""
    >
    > me = person()
    >
    > # store
    > pf = file('/tmp/pickletest', 'w')
    > pickle.dump(me, pf)
    > pf.close()
    >
    >
    > # load
    > pf = file('/tmp/pickletest', 'r')
    > me2 = pickle.load(pf)
    > pf.close()
    >
    > This is sequential access. If you want to have random access, look
    > for shelve.
    >




    --
    Using Opera's revolutionary e-mail client: http://www.opera.com/m2/
    --------------------------------------------------------------------
    Please find our disclaimer at http://www.ukzn.ac.za/disclaimer
    --------------------------------------------------------------------
    <<<<gwavasig>>>>
    Johan Kohler, Jan 26, 2005
    #3
    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. Ben Finney

    Securing 'pickle'

    Ben Finney, Jul 11, 2003, in forum: Python
    Replies:
    17
    Views:
    563
    Paul Rubin
    Jul 11, 2003
  2. Aki Niimura

    freeze utility and pickle

    Aki Niimura, Aug 21, 2003, in forum: Python
    Replies:
    1
    Views:
    523
    =?ISO-8859-1?Q?Gerhard_H=E4ring?=
    Aug 21, 2003
  3. Terry Reedy

    Re: "pickle" vs. f.write()

    Terry Reedy, Jan 26, 2005, in forum: Python
    Replies:
    4
    Views:
    350
    Martin Miller
    Feb 6, 2005
  4. a pickle's pickle

    , Aug 2, 2005, in forum: Python
    Replies:
    4
    Views:
    373
  5. Michele Simionato
    Replies:
    2
    Views:
    1,862
    Michele Simionato
    May 23, 2008
Loading...

Share This Page