[Tutor] Alternative File I/O for Tuples

Alan G alan.gauld at freenet.co.uk
Sun Jun 26 16:09:39 CEST 2005

> arranged more like it is on the console screen - in tabbed columns.
None of
> the tutorial type stuff I've seen even mentions printing files,

One reason is that printing is one of those things that
is different on evry operating system. So tutorials
(including mine) tend to steer clear of it.

If you are on Unix its fairly easy to print stuff just by
piping stdout to the lpr or pr commands. On Windows I
tend to print by creating an HTML file and then using
os.system() to print the file using my browser...

On Mac tyou can use the Unix trick or convert to PDF.

To do proper formatted printing on windows is inordinately
difficult and involves effectively printing your page as
a graphic to a printer "device context". I seem to recall
someone pointing out a printer module which I meant to
investigate but never got round to it, maybe trawling the
archives would throw it up.

> Here is some sample data from the resulting file:
> ((S'Everybody'
> S'Anonymous'
> Nt(S'James'
> S'Austin'
> S'704-111-1234'
> t(S'Janet'
> S'Austin'
> S'704-111-1234'
> I would like to see something more like when the file is printed:
> Austin    James    704-111-1234
> Austin    Janet    704-111-1234
> etc.

This is to do with formatting the data into strings before
you print it. You can use a format string to define the
layout(column widths, justification, number of decimal places etc)
andthen send those lines to the printer as described above.
If using the HTML trick you need to add the HTML codes too,
in this case you would format each row like:

fmt = "<tr><td>%10s%10s%3d-%3d-%4d</td></tr>"

And then print a table header followed by a loop printing
each line of data followed by a close table tag, something
like this:

text = '''
<tr><th>Last</th><th>First</th><th colspan='3'>Phone</th></tr>'''

for data in people:   #substitute your data here
  text += fmt % (data[0],data[1],data[2],data[3],data[4])

text += '</table></body></html>'

prtfile = open('temp.html','w')

os.system('lynx -p ./temp.html')

> Is this a simple task, or am I jumping into deep water? :)
> evangelinux    GNU Evangelist

Given your signature its probably not too difficult,
you can just send the basic text string to lpr, or use the
html trick above.

You might even like to investigate the use of groff to
format the text instead of html - that's much nicer and
the technique I actually use on Unix.


Alan G
Author of the Learn to Program web tutor

More information about the Tutor mailing list