[Tutor] Need help printing a pickled data

Prasad, Ramit ramit.prasad at jpmorgan.com
Tue Jun 25 19:15:37 CEST 2013


Please leave attributions in so we know who is saying what.

Matt D wrote:
> [Ramit Prasad wrote]
> > [Peter Otten wrote]
> >
> > with open('mypicklelog.txt','ab') as log: # open in binary mode
> >     pickle.dump(self.data, log) # serialize data and write to file
> >
> > where pickle.dump(obj, file) converts `obj` to a sequence of bytes before it
> > is written to `file`.
> >
> 
> I put this like this:
> 
>  class DataEvent(wx.PyEvent):
>     # the values of the text fields get passed into the constructor
> inside of data
>     def __init__(self, data):
>         wx.PyEvent.__init__(self)
>         # this line *binds* this class to a certain type of event,
> wxDATA_EVENT
>         self.SetEventType (wxDATA_EVENT)
>         # and this is the actual data
>         self.data = data
>         with open('mypicklelog.txt','ab') as log: # open in binary mode
>             pickle.dump(self.data, log) # serialize data and write to file
> 
> 
> And I still get nothing.  This has me super confused, I have trying at
> this for a long time and I have been reading the docs like
> 
> http://docs.python.org/2/library/pickle.html
> 
> And I still don't get it to work. So the C++ makes the pickle, this is
> from the comments in the C++ :
> 
> /**
>  * snapshot_du_handler. Writes traffic snapshots to a msg_queue based
>  * on the HDU frame contents. The format used is that of a pickled
>  * python dictionary allowing the other end of the queue to pick only
>  * those fields of interest and ignore the rest.
>  */
> 
> Then the python program (attached) program unpickles and repickles it?
> I attached the python program, if you have a minute or two please take a
> look, its not too long.

The problem is that what gets sent across the queue is actually a container 
of some kind that has a pickled string. The comments in the code are just 
wrong. The key is following display_data which is correct.

# unpickle the string 
pickled_dict = message.to_string()

This is actually retrieving a string (or bytes more accurately) OF pickled data 
from some message or container object. These bytes then needs to be unpickled.

#separate the string into values for each text control (attrs is a pickle object)
attrs = pickle.loads(pickled_dict)

Here the pickled data (as bytes/string) are unpickled and attrs
is a Python dictionary (not a "pickle object").

*I use bytes/string interchangeably because in Python 2 they are the same. 
In Python 3 and semantically, they are different so the differentiation is 
important.

The real question is why do you want this pickle in a file?  I am not sure 
it will be easy to pull out and reuse anyway. Given your experience level, 
I think this is a lot of work for something that you are unlikely to be able 
to easily use. I think it would be more useful to `log.write(repr(attrs))`.

Once you have the bytes of pickled data, just write that to file.

with open('mypicklelog.txt','ab') as log: 
# All pickles will run together because there is no spacing.
    log.write(self.data.to_string()) 


Again, I think you would be better off with the repr()
with open('mypicklelog.txt','ab') as log: 
    log.write(repr(pickle.loads(self.data.to_string())))


~Ramit


This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  


More information about the Tutor mailing list