Mailing List Archive

extracting contents from a text file
Hi,

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.

Thanks and regards
John
extracting contents from a text file [ In reply to ]
>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, '<', '&lt;')
line = string.replace(line, '>', '&gt;')
out.write(line)
out.write(footer)
extracting contents from a text file [ In reply to ]
In article <375B5880.992B0C6C@mediamanager.com.sg>, john@mediamanager.com.sg
says...

>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.

You need three separate modules to
1. retrieve text file (1 of url libs or possible FTP lib);
2. process file (hope string or re rather than parser is sufficient);
3. write html (for instance, HTMLgen on starship site -- see python.org).

TJ Reedy