[Tutor] Saving and loading data to/from files
Daryl G
gman_95@hotmail.com
Sun, 03 Jun 2001 23:05:11 -0000
Whoever is "with it" on a Sunday? ;)
I originally had it set up as a dictionary, but when I was displaying them,
it wasn't coming out right and I see why now.
I will eventually use classes, I'm just getting the basic functionality down
first.
I was trying the dictionary example in a seperate script, but I get a
traceback error
File "C:\Python20\dict.py", line 7, in ?
print "%-15.15(last)s %-15.15(first)s %(phone)s" % record
ValueError: unsupported format character '(' (0x28)
Python ver 2.0
The tuple example works.
I like this one, but tuples aren't mutable, thats why I -reluctantly- chose
lists when I couldn't get the dictionary to display how I wanted to. That
way if say a phone number changes, I just need to edit the phone number and
not have to delete that record and reenter it all in
But I'll implement that later
Cool!
Daryl
>From: "Michael P. Reilly" <arcege@dsl092-074-184.bos1.dsl.speakeasy.net>
>Reply-To: arcege@speakeasy.net
>To: gman_95@hotmail.com (Daryl G)
>CC: tutor@python.org
>Subject: Re: [Tutor] Saving and loading data to/from files
>Date: Sun, 3 Jun 2001 17:09:07 -0400 (EDT)
>
>Daryl G wrote
> > That was it, though I did have to do it a little differently.
> > The way you showed didn't seem to work with lists, so I just converted
>each
> > individual part of a list into a String and then it worked
> >
> > for i in range(total):
> > LN=L_Name[i]
> > FN=F_Name[i]
> > PH=PH_num[i]
> > print"%-15.15s %-15.15s %-s" % (LN, FN, PH)
>
>Sorry, I hadn't quite gotten that they were lists (I haven't really been
>feeling "with it" lately).
>
>You might find it easier to keep things together. Create tuples with
>the information, then arrays of those tuples.
>NameInfo = [
> ('Guido', 'van Rossum', '703-555-1234'),
> ('Eric', 'Idle', '212-555-5346'),
>]
>for i in range(len(NameInfo)):
> (FN, LN, PH) = NameInfo[i]
> print "%-15.15s %-15.15s %s" % (LN, FN, PH)
>
>This will allow for keeping things together later, and for better
>looping structures: `for (FN, LN, PH) in NameInfo:'
>
>
>Alternatively, you could use a dictionary for records of "named" fields:
>NameInfo = [
> {'first': 'Guido', 'last': 'van Rossum', 'phone': '703-555-1234'},
> {'first': 'Eric', 'last': 'Idle', 'phone': '212-555-5346'},
>]
>for record in NameInfo:
> print "%-15.15(last)s %-15.15(first)s %(phone)s" % record
>
>
>Or maybe make a class:
>class Record:
> def __init__(self, first, last, phone):
> self.first, self.last, self.phone = first, last, phone
> def __str__(self):
> return '%-15.15s %-15.15s %s' % (self.last, self.first, self.phone)
>NameInfo = [
> Record('Guido', 'van Rossum', '703-555-1234'),
> Record('Eric', 'Idle', '212-555-5346'),
>]
>for record in NameInfo:
> print record
>
>There are a lot of choices for you, but separate lists for each field
>probably isn't a good one. :) (Incidently, the pickle and shelve modules
>handle all these structures)
>
> -Arcege
>
>--
>+----------------------------------+-----------------------------------+
>| Michael P. Reilly | arcege@speakeasy.net |
_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com