[Tutor] To write data in two different fonts?
Dave Angel
davea at ieee.org
Tue Aug 11 17:41:38 CEST 2009
prasad rao wrote:
> Hello
> I modified my code.But I am gettingmy my lines stripped of indentation.
> What should I do to avoid it?Sorry if it is not about Python.
>
> <code>
>
>
> def sc(adir):
> import os,myfiles
> dest=open('C:/scripts.html','w')
> s=myfiles.myfiles(adir)
> dest.write('<html>')
> for x in s:
> if os.path.isfile(x):
> sorce=open(x,'r')
>
> dest.write('<h1><font=Helvetica,fontsize=14>')
> dest.write(x)
> dest.write('</h1>')
>
>
> for l in sorce:
> dest.write('<p><font=Arial,fontsize=8>')
> dest.write(l)
> dest.write('</p>')
> sorce.close()
> else:pass
> dest.write('</html>')
> dest.close()
>
> </code>
>
>
This question is about html. You knew enough to put the html tags
around it, and the p tags around each line. But within a p tag, the
element content is transparent to whitespace. That's usually a good
thing, as it means paragraphs automatically wordwrap according to the
size and shape of the browser. But you want the text here to be
unformatted, preserving indents, and preserving embedded spaces.
<following untested>
The brute-force way might be to replace each space in "l" with
which is a "nonbreaking space." But I think you want the <pre>
tag, which means the text is pre-formatted. And you could put that
around the whole sorce file, instead of doing <p> for each line.
See http://www.w3schools.com/tags/tag_pre.asp
Incidentally, I think you ought to add <body> as well. And maybe some
other boilerplate to eliminate warnings in a browser. You'll also get
into trouble if any of the lines have "&" or "<" in them.
DaveA
More information about the Tutor
mailing list