[Tutor] Alternative File I/O for Tuples (fwd)

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Sun Jun 26 22:08:35 CEST 2005



---------- Forwarded message ----------
Date: Sun, 26 Jun 2005 14:29:22 -0400
From: Don Parris <webdev at matheteuo.org>
To: Danny Yoo <dyoo at hkn.eecs.berkeley.edu>
Subject: Re: [Tutor] Alternative File I/O for Tuples

On Sat, 25 Jun 2005 22:52:08 -0700 (PDT)
Danny Yoo <dyoo at hkn.eecs.berkeley.edu> wrote:

>
>
> > In your example, it looks like every row is ten characters long.  A
>                                        ^^^
> Hi Don,
>
> Gaa, that's why I'm so bad at matrix math.  I meant "column", not "row".
> Sorry about that.


I guess I should have used a different example - some names are longer than
others.  Williams is shorter than Vandenbosch, for example.  So the columns
will need to be formatted appropriately.  I'll play with this a bit.  I
should also have said that I need to work primarily with GNU/Linux -
printing with Windows can come a little later.


Kent is kind of on track.  I let the user view the report on the console
screen (nice to verify what you're about to print), and then give them the
choice of printing it, or returning to the menu for other tasks.  For
instance, they may simply want to look up a number, not print out the phone
list.

### Print or Return to Menu ###
    if whatNext == 'p':
        os.system("lpr mbrPhone.txt")    # prints the pickled file.
    elif whatNext == 'r':
        pass                    # BTW, is this a good use of "pass"?

The screen print out is as it should be - nice, neat columns.  So, instead
of this:

    print 'Phone List'
    for record in Results:
        print '%s\t%s\t%s' % record    # prints to screen
        mbrPhone = open('mbrPhone.txt', 'w')
        cPickle.dump(Results, mbrPhone)    # pickles the data into a file
        mbrPhone.close()


I tried this:
mbrPhone = open('mbrPhone.txt', 'w')
mbrPhone.writelines(repr(Results))
mbrPhone.close()



Which produces this in the text file:
(('Everybody', 'Anonymous', None), ('James', 'Austin', '704-111-1234'),
('Janet', 'Austin', '704-111-1234'), ('James', 'Austin', '704-111-1234'),

This is an improvement over pickling the data into a file.  Still, it's not
quite the columnar layout I'd like to achieve.  I'll try that recipe that
was suggested to see if I can get it working with files.  If it's the one
I'm thinking of, it offers a fantastic layout ('bout as good as it gets with
ASCII text) for printing out to screen.

I guess I'm asking two closely-related questions:
(1) how to format the file so it can be readable, and
(2) how to send that file to the printer.  I used os.system('lpr, filename')
above.  Is there another way?


Don
-- 
evangelinux    GNU Evangelist
http://matheteuo.org/                   http://chaddb.sourceforge.net/
"Free software is like God's love - you can share it with anyone anytime
anywhere."



More information about the Tutor mailing list