[Tutor] Saving class data to a text file
darryl at snakegully.nu
Sat Jul 16 17:05:21 CEST 2005
Hi all. I'm looking at Python for the first time, and using it for an
app I'm writing to try it out. I normally use c or perl.
I have a class that has half a dozen or so data fields. It needs to be
able to save its data to a text file, in a format that's easy to edit
with other tools (vi, perl, etc). And needs to be able to load itself
back from disk.
I discovered that a dictionary can convert itself into a nice string
format that I can save directly to a file. So I thought I could put a
dictionary in the class to hold the data, and save the string
representation of the dictionary to the file. Each object instance is on
a single text line. I created get/set methods for each field to avoid
problems if I mis-type the dictionary keys.
Now I have a couple of questions:
- What is the recommended 'python' way to implement this class? and
- If what I'm doing is OK, is there an easy way to load a dictionary
from the string representation without parsing the string manually?
Thanks for your time!
More information about the Tutor