"pickle" vs. f.write()

From: Johan Kohler (kohlerj_at_ukzn.ac.za)
Date: 01/26/05


Date: Wed, 26 Jan 2005 12:16:01 +0200
To: python-list@python.org

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


Relevant Pages

  • Re: A Logical Model for Lists as Relations
    ... I am aware of the difference between the number zero and an empty list. ... contents of a set - yields a bunch, which also has a comma union ... is a string of length 4. ... Empty strings and empty lists? ...
    (comp.databases.theory)
  • Filling out HTML form w/ hidden and pre-selected fields
    ... I've scraped an HTML page and gotten back a string that looks like this: ... bunch of stuff I don't care about ...] ... Ideally, the hash would include the field type, form action URL, etc, ...
    (perl.beginners)
  • Re: marshal bug?
    ... Any string in Python can be "interned" or not, ... considered a misuse of the marshal module. ... So maybe I have to change all my code to use pickle, ...
    (comp.lang.python)
  • Re: How to assign a block of text to a string in C#
    ... Also if it comes down to it you could right a very basic program that auto ... >This is a bunch of text ... >that is assigned to string x ... Just adding all the quotes is tedious. ...
    (microsoft.public.dotnet.languages.csharp)
  • Opening filenames from a list in a spreadsheet
    ... I am writing a program for use at a bunch of sites around the world. ... uses several SAP extracts that are to be stored on the C: ... I know I need to use a string but am new to the world of strings. ... Dim CWDBXLS ...
    (microsoft.public.excel.programming)