"pickle" vs. f.write()

Peter Maas peter at somewhere.com
Wed Jan 26 06:55:38 EST 2005


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')
-------------------------------------------------------------------



More information about the Python-list mailing list