extracting contents from a text file

Peter Haight peterh at sapros.com
Mon Jun 7 02:57:35 EDT 1999


>I would like to extract the text contents residing in another server and
>display it as html format using python. Can anyone help me I am new to
>python.

Well, here's an example. I'm assuming that the 'text contents' you are
talking about is on an HTTP (web) server. I'll grab the Python home page as
an example at 'http://www.python.org/' but you could easily modify this
for other kinds of servers and documents. It writes the html to stdout. You
won't need the 'string.replace' lines if your 'text contents' are not
formatted as html.

--------------------
import string
import sys
import urllib

file = urllib.urlopen('http://www.python.org/')

header = \
"""
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html>
<head>
<title>Example</title>
</head>
<body>
<pre>
"""

footer = \
"""
</pre>
</body>
</html>
"""

out = sys.stdout

out.write(header)
for line in file.readlines():
	#
	# This keeps a browser from parsing an html file.
	#
	line = string.replace(line, '<', '<')
	line = string.replace(line, '>', '>')
	out.write(line)
out.write(footer)





More information about the Python-list mailing list