Pickle and wx.TextCtrl

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Thu Jul 17 19:44:28 EDT 2008


En Thu, 17 Jul 2008 09:40:29 -0300, <DWebre at dotd.la.gov> escribi�:

> The way you read the file is rather strange -mixing calls to readline and
> pickle.load- I'd write the data using pickle.dump calls *only* and then
> read it using pickle.load calls *only*.
>
> I used 13.1.7 Example of the Python Library Referencebut.  Got an error
> message when I did not have the readline() statement.

The example doesn't have any readline(). Make sure you open the file in  
binary format ('wb' or 'rb').
If you have a list of objects: just write the list. It takes a single call  
to pickle.dump(), and later, a single call to picle.load()
If you have too many objects and don't want to save/load all of them at  
once, write them one at a time using pickle.dump(element, output_file,  
-1), ending with a sentinel value (e.g. None): pickle.dump(None, ...)
You may read them again using something like this:

pfile = open(..., 'rb')
while True:
   element = pickle.load(pfile)
   if element is None: break
   do_something_with(element)
pfile.close()

Ensure that you can save and load your data using a simple script,  
*before* writing the GUI.

-- 
Gabriel Genellina




More information about the Python-list mailing list