translate txt into HTML

Mike Meyer mwm at
Tue Jan 21 19:11:18 CET 2003

Peter Hansen <peter at> writes:

> Natacha wrote:
> > 
> > We have to create a program allowing to go to seek two textual files
> > bruts(.txt), but we would like to translate them into HTML.
> > 
> > we think that a parser should be used, but we do not know how to make!
> To help you clarify your requirements, I offer this first version which
> meets your requirements as stated.  What aspect of this do you find
> insufficient?  If you can explain that, we can help you with a more
> suitable solution, perhaps involving the StructuredText concept...
> text = open('afile.txt').read()
> htmlFile = open('afile.html', 'w')
> print >>htmlFile, '<html><head><title>A file</title></head>'
> print >>htmlFile, '<body><pre>%s</pre></body></html>' % text
> htmlFile.close()

You've got a bug. Try this for the second print:

print >>htmlFile, '<body><pre>%s</pre></body></html>' % text.replace('<', '<')

Mike Meyer <mwm at>
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.

More information about the Python-list mailing list