"pickle" vs. f.write()

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Johan Kohler

    "pickle" vs. f.write()

    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>>> >
  • Peter Maas

    #2
    Re: &quot;pickle&qu ot; vs. f.write()

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

    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 'cGV0ZXIubWFhc0 BtcGx1c3IuZGU=\ n'.decode('base 64')
    -------------------------------------------------------------------

    Comment

    • Johan Kohler

      #3
      Re: &quot;pickle&qu ot; vs. f.write()

      Thanks... works like a charm :-)

      On Wed, 26 Jan 2005 12:55:38 +0100, Peter Maas <peter@somewher e.com> wrote:
      [color=blue]
      > Johan Kohler schrieb:[color=green]
      >> class person:
      >> name =""
      >> age = 0
      >> friends=[]
      >> comment=""""""
      >> me = person()
      >> Otherwise, what is the best "Python" way to write and read this data
      >> structure?[/color]
      >
      > 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.
      >[/color]



      --
      Using Opera's revolutionary e-mail client: http://www.opera.com/m2/
      --------------------------------------------------------------------
      Please find our disclaimer at http://www.ukzn.ac.za/disclaimer
      --------------------------------------------------------------------
      <<<<gwavasig>>> >

      Comment

      Working...