[Tutor] Need help printing a pickled data

eryksun eryksun at gmail.com
Tue Jun 25 13:28:44 CEST 2013


On Mon, Jun 24, 2013 at 6:57 PM, Steven D'Aprano <steve at pearwood.info> wrote:
>
> 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:

I don't know which version Matt is using, but 2.x defaults to pickle
protocol 0, which is ASCII. In 3.x you can manually specify protocol
0:

    >>> pickle.dumps([1,2,3], protocol=0)
    b'(lp0\nL1L\naL2L\naL3L\na.'

With the ASCII protocol you can open the file in either text or binary
mode, so long as you're consistent. But use binary mode if it needs to
be cross-platform.


More information about the Tutor mailing list