translate txt into HTML

Bengt Richter bokr at
Wed Jan 22 02:17:30 CET 2003

On Tue, 21 Jan 2003 18:11:18 GMT, Mike Meyer <mwm at> wrote:

>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('<', '<')
I think you have a bug too ;-)

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

Or else, e.g., you couldn't show this post as text.

Bengt Richter

More information about the Python-list mailing list