Editing File

Tal Einat taleinat at gmail.com
Wed Jul 12 12:02:00 EDT 2006


akameswaran <at> gmail.com <akameswaran <at> gmail.com> writes:

> 
> 
> D wrote:
> > Thanks, guys.  So overall, would it just be easier (and not too rigged)
> > if any changes were made by just editing the text file?
[snip]
> have you used pickle?  if the data is as simple as you say it is, you
> will be able to read the pickle file.  2nd, it will be a lot less code
> really.  You just load and unpickle the file into a variable.  After
> any edit in the gui, repickle it to the same file.  You would have to
> do the same if you edited the text file, except you would need a couple
> of lines code to parse the string, etc.
> 

If you don't plan to edit your data by hand, Pickle is a nice and simple choice.
To save you from coding the persistency bits, you can use Python's built-in
shelve module.

Shelve uses Pickle to serialize Python objects but it does most of the
persistency stuff for you. It is very simple and clean to use. One nice feature
is that can open a shelve in auto-writeback mode, so upon assignment to one of
the shelve's keys (it's like a dict) the shelve is automatically re-serialized
to the file on disk. This saves headaches like remembering to serialize the data
upon exit cleanup, crashes resulting in data loss, etc.

Shelve is the simplest tool I know of. YAML sounds nice and readable, I haven't
tryed it out yet. You could use XML easily enough with ElementTree.

Good luck!

- Tal






More information about the Python-list mailing list