[Tutor] Pickling files in Python

Walter Prins wprins at gmail.com
Sat Nov 19 14:42:18 CET 2011


Hi Joe,

On 19 November 2011 13:28, Joe Batt <joebatt at hotmail.co.uk> wrote:

> I am new to programming and on a very steep curve. I am using Python 3 to
> learn and I am having trouble understanding exactly what pickling is.
> I have written a program that asks for a URL and then writes the source
> code from the URL to a file, then pickles the file I just created.
> I am not sure why I pickle something as opposed to just saving it as a
> normal file?
>

Pickling is a way for you to write arbitrary Python objects (or object
structures) to file without having to think about their structure when
writing (and read them back as well obviously).  In general programming
terms this is often referred to as serializing and deserializing.  Anyway,
it follows then that if you're handling the writing of data to a file
yourself, such as when you're directly downling the textual content of a
web page and writing that directly to a simple text file "by hand", that
pickling is redundant/not relevant to your intent.  So, either you write
and read the text yourself, or (for example) you put the web page data into
a list and then pickle (or unpickle) the list object to/from a file using
Python's pickling support.

Aside:  If (as I'm suspecting) you're busy with puzzle 5 on the Python
challenge, then I'll given you this hint:  IIRC, there's a pickle file that
can be downloaded from the Python challenge website, hidden somewhere in
the puzzle 5 page (referenced in it directly or indirectly) that you're
supposed to be interrogating in order to solve the puzzle.  You're not
supposed to be doing any pickling of the base web page of puzzle 5 at all.
You *are* supposed to get the pickle data from the site, get it loaded up
into Python (e.g. make it back into a Python object) and then figure out
what it contains.

Hope that helps,

Walter
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20111119/6bf4500a/attachment.html>


More information about the Tutor mailing list