Extracting Text file contents using Python
john
john at mediamanager.com.sg
Mon Jun 28 07:37:50 EDT 1999
Hi,
I am trying to extract the following text file using python and the program
& text file are as shown below.
But unfortunately I am unable to get every separate data displayed except
for the first data and after the separation line which is
"====================" the next data is not getting picked up by the
program.
Could anyone kindly let me know what could be wrong with the program.
Example Text File - But will be similar
----------------------------------------------------
Title:Meet Me
Http Host:www.www.www
Remote Address:10.2.0.1
Remote Host:10.2.0.1
Rating:8
Date:06/25/99
Time:17.15.28
=================================
Title:Meet Me
Http Host:www.www.www
Remote Address:10.2.0.1
Remote Host:10.2.0.1
Rating:8
Date:06/25/99
Time:17.15.28
=================================
Title:Meet Me
Http Host:www.www.www
Remote Address:10.2.0.1
Remote Host:10.2.0.1
Rating:8
Date:06/25/99
Time:17.15.28
=================================
Title:Meet Me
Http Host:www.www.www
Remote Address:10.2.0.1
Remote Host:10.2.0.1
Rating:8
Date:06/25/99
Time:17.15.28
=================================
Title:Meet Me
Http Host:www.www.www
Remote Address:10.2.0.1
Remote Host:10.2.0.1
Rating:8
Date:06/25/99
Time:17.15.28
=================================
The python program is as follows:
#!c:/progra~1/python
import string
import sys
import cgi
#import urllib
data_file = open('c:/xitami/cgi-bin/textfiles/FilmFestReview.txt', 'r')
form = cgi.FieldStorage()
header = \
"""
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html>
<head>
<title>Extract & Write</title>
</head>
<body>
"""
footer = \
"""
</body>
</html>
"""
out = sys.stdout
begin_table = 1
out.write(header)
for line in data_file.readlines():
if begin_table:
out.write('<table border = "1">')
begin_table = 0
if line[:5] == "=====":
out.write('</table>')
out.write('<br>')
begin_table = 1
continue
field_name, field_value = string.split(line, ':')
out.write('\t<tr>\n')
out.write('\t\t<td>\n')
out.write('\t\t\t%s\n' % field_name)
out.write('\t\t</td>\n')
out.write('\t\t<td>\n')
out.write('\t\t\t%s\n' % field_value)
out.write('\t\t</td>\n')
out.write('\t</tr>\n')
out.write(footer)
More information about the Python-list
mailing list