[Tutor] newbie intro to pickle
D. Hartley
denise.hartley at gmail.com
Sat Apr 16 03:57:47 CEST 2005
Thanks, Tony, your example is much clearer (to me!) than that on the
python page. A couple quick questions about it:
So the .dump command is, in effect, saving the file, correct? which
takes the object you're saving (in my case it would be
high_scorelist), and ("filename","..... what is the "w" ?) Why does
the temp file you're saving into end in .pik?
Then import is, in effect, loading the file (the score list) from
wherever you saved it. do you have to give it a new name? (i.e.,
"my_list_reloaded"?) I'm thinking not. i could import the file right
before the high score list is displayed in the game, and dump right
after a new score is added/the list is changed?
Thanks for creating an easy-to-follow example!
:)
~Denise
On 4/15/05, Tony Meyer <tameyer at ihug.co.nz> wrote:
> > Does anyone know if there are some *beginner*-user-friendly tutorials
> > out there for pickle? Or can give a sample of how you would implement
> > it into a VERY SIMPLE program? Obviously I can get to the "import
> > pickle" step, but once I'm past that, I'm totally lost!
>
> There's the example in the documentation:
>
> <http://docs.python.org/lib/pickle-example.html>
>
> But to put it really simply, you can just use it like this:
>
> >>> import pickle
> >>> my_list = [4,5,6,6]
> >>> pickle.dump(my_list, file("temp.pik", "w"))
>
> (restart Python here to show that it works across sessions)
>
> >>> import pickle
> >>> my_list_reloaded = pickle.load(file("temp.pik"))
> >>> my_list_reloaded
> [4, 5, 6, 6]
>
> =Tony.Meyer
>
>
More information about the Tutor
mailing list