On Mon, Jun 24, 2013 at 06:05:37PM -0400, Matt D wrote:

> I have been unable to find a way to write pickled data to text file.

Normally you write pickled data to a file like this:

import pickle

data = {'something': 'goes', 'here': 42}
with open("/path/to/file/name.pickle", "rb") as f:
    pickle.dump(data, f)


And then read it back again like this:

with open("/path/to/file/name.pickle") as f:
    data = pickle.load(f)


You certainly shouldn't be writing pickle data to a log file! Firstly, 
log files are usually opened in text mode, not binary mode, so it 
probably won't work, and secondly even if it did work, you will be 
dumping a load of pickled binary data into the middle of what should be
a text file. That's a bad idea. And even if it succeeded, what are you  
going to learn from seeing a line like this:

\x80\x03}q\x00(X\x04\x00\x00\x00hereq\x01K*X\t\x00\x00\x00somethingq\x02X\x04\x00\x00\x00goesq\x03u.
 

in the middle of your log file? Log files are supposed to be for humans 
to read, not for binary data like a pickle.


> My last attempt was to add the last two lines:
[...]
> I cant figure out why these last two line dont write to the .txt file
> after the program has received the pickled Python dictionary?  Very
> stumped.

What does it do? Do you get an error?


-- 
Steven
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to