[Tutor] Saving data to be retrieved later.

Erik Price eprice@ptc.com
Fri Feb 21 09:50:01 2003


Chris Bailey wrote:
>  Hey everyone, I'm a bit new to python, and I don't know much more than 
> the basics of programming in general.
> I'm trying to find a bit of help, but websites/tutorials haven't been 
> very informative in the ways I need them to be.
> Basically what im trying to do, is save a bunch of statistics 
> to seperate files, so they can be called on later. If
> you are familiar with muds, i'm trying to work with pfiles. I just 
> thought I would make a single player text based
> game for my friends and I to play. I have some neat ideas, and I think I 
> know how to implement them, But I
> will of course need a way to save our characters! :) 

If you just want to quickly serialize Python objects, the cpickle module 
will do the trick conveniently.  However, if you wanted to store the 
data in a more versatile format (say if you have other programs not 
written in Python that needs to parse the data), you could just write 
some XML handlers and do it that way.  Or, if your data doesn't need a 
hierarchical structure (say it's just a dictionary of name/value pairs), 
  you could literally just write these to a file like so:

name=Fizban
class=Wizard
spell=Fireball
spell=Magic Missile

(of course, if you really want to use multiple properties with the same 
name, your "read properties" code should know to put the data into a 
sequence or list rather than overwrite the values, etc)

Just some ideas.

Erik