473,394 Members | 1,567 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

"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>>>>
Jul 18 '05 #1
2 1732
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')
-------------------------------------------------------------------
Jul 18 '05 #2
Thanks... works like a charm :-)

On Wed, 26 Jan 2005 12:55:38 +0100, Peter Maas <pe***@somewhere.com> 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>>>>
Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Grzegorz Adam Hankiewicz | last post by:
Hi. The python debugger is fine, when you are at the machine with the problematic script. Now, how about postmortem remote debugging? Is it possible to pickle somehow an interactive session,...
2
by: Vio | last post by:
I would like to know if executing: c = compile('a=5\nprint a','<string>','exec') on a Linux box, then pickling+beaming the result on a Windows box, will give me the expecting result: >>>...
33
by: Steven Bethard | last post by:
I feel like this has probably been answered before, but I couldn't find something quite like it in the archives. Feel free to point me somewhere if you know where this has already been answered. ...
5
by: engsolnorm | last post by:
I'm playing with a sudoku GUI...just to learn more about python. I've made 81 'cells'...actually small canvases Part of my scheme to write the cells (all 81 of them in the gui) to a file (using...
28
by: robert | last post by:
In very rare cases a program crashes (hard to reproduce) : * several threads work on an object tree with dict's etc. in it. Items are added, deleted, iteration over .keys() ... ). The threads are...
5
by: Peter Hansen | last post by:
I'm investigating a puzzling problem involving an attempt to generate a constant containing an (IEEE 754) "infinity" value. (I understand that special float values are a "platform-dependent...
0
by: Jeff Groves | last post by:
I'm using FreezePython on a Python program that uses wxPython and subprocess. The result almost works, but it always hits this bug: File "velauncher.py", line 847, in Launch File...
2
by: RayOsborn | last post by:
I have some old "shelve" databases created in Python 2.2 that use the old bsddb format, whereas the version of Python 2.4 installed by my web hosting service doesn't have bsddb available at all...
0
by: Edwin.Madari | last post by:
since choice of dbm used by shelve http://docs.python.org/lib/node327.html depends on os, and whats available on it, shevle files saved on one os, most likely do not work on another os, sometimes on...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.