[Tutor] I need help with saving to files

Daniel Yoo dyoo@hkn.eecs.berkeley.edu
Thu, 22 Mar 2001 07:55:49 -0800 (PST)


On Thu, 22 Mar 2001, Matthews James wrote:

> This is what i need help on
> 
> #####################################################
> # File: JacksRecords.py
> 
> def print_slip():
>     print '_______________________________'
>     print ' ',Name,',   ',Date
>     print 'EMP #    CAT      IN'
>     print Empnum,'     ',CAT,'   ',FIRSTIN
>     print '                  IN     OUT'
>     print '               ',FIRSTIN,' ',FIRSTOUT
>     print '                  IN'
>     print '               ',SECIN
>     print '                  IN     OUT'
>     print '               ',SECIN,' ',SECOUT
>     print '-------------------------------'


Another way to do this is to change your print_slip() into a get_slip():
instead of having it print the slip out, you can write a function that
returns a string:

def get_slip():
     return '''_______________________________
  %s   %s
EMP #    CAT      IN'
%s       %s       %s
                  IN     OUT
                  %s     %s
                  IN
                  %s
                  IN     OUT
                  %s     %s
-------------------------------''' % (Name, Date, 
                                      EmpNum, CAT, FIRSTIN,
                                      FIRSTIN, FIRSTOUT,
                                      SECIN,
                                      SECIN, SECOUT)

For conciseness, we're using triple-quoted strings, which go on until we
hit another set of triple quotes --- useful when we're building
forms.  Also, the function above does something really interesting called
string interpolation: wherever you see '%s', it tries to fill in with an
appropriate variable.

(For more information on these two topics, take a look here:)

http://python.org/doc/current/tut/node9.html#SECTION009100000000000000000


The reason this function is nice is because now it can be used in both
file writing and in printing to the screen:

    print get_slip()
    some_file.write(get_slip())

can work with get_slip() equally well.  It also "feels" less cluttered,
although I know that's a subjective opinion.



>  I want to beable to save 'print_slip()' to a file
> and then add more to that same file.

Daniel Coughlin mentioned how to open files so that appending --- adding
stuff to a file's end --- works out well.




>  I also want it to look like this when it saves the
> file
> _____________________________________________
> John Doe,              3/21/2001
> EMP #      CAT       IN
> 4578       1         7:54
>                      IN           OUT
>                      7:45         8:25
>                      IN        
>                      8:55
>                      IN           OUT
>                      8:55         23:54
> ---------------------------------------------


You'll want to experiment with string interpolation: it gives you fine
control over how many spaces something takes in.  For example, here's a
small interpreter session that gives a taste of what this means:

###
>>> for i in range(10):
...     print '%5s  %5s  %5s' % (i, i**2, i**3)
...
    0      0      0
    1      1      1
    2      4      8
    3      9     27
    4     16     64
    5     25    125
    6     36    216
    7     49    343
    8     64    512
    9     81    729 
###

Instead of just putting '%s', now we have '%5s', which tells Python: "This
will take at least 5 spaces worth of characters to print, so leave that
much space for us to work with."  If you fiddle around with this, you'll
be able to get a nicely indented form.  It's also mentioned in the URL:

http://python.org/doc/current/tut/node9.html#SECTION009100000000000000000

around halfway through that page.


(On the other hand you might consider having your program output something
besides plain text: you can have it spit out HTML.  Doing this will be
nice, since HTML has table-formatting commands that should be easier to
work with than counting spaces.)


(Also, you might want to save your timestamps in two different ways: one
that's easy for the computer to understand, and one that's easy for you to
read.  Although it might seem redundant, it might also be useful, if you
ever want to write programs that study or search your time cards.)


Good luck to you!