[Tutor] string.replace

Jon Cosby jcosby@mindspring.com
Tue, 11 Sep 2001 17:08:15 -0700


Thanks for the help so far. Here's one more that has me stumped. I need a
function to strip HTML tags from lines. What I have looks like it should
work, but it stops at the first occurence of the tags. I've gone through it
step by step, and it's finding all of the tags, but for some reason it's not
replacing them. The routine is


>>> from string import *
>>> def striptabs(line):
... 	if "<" in line:
... 		l = find(line, "<", 0)
... 		m = find(line, ">", l)
... 		if l and m:
... 			print "Tag found"		# Test
... 		line = replace(line, line[l:m+1], "")
... 		striptabs(line)
... 	else:
... 		return line
... 	return line
...
>>> f = open("c:\\temp\\test.txt", "r")
>>> l = f.readline()
>>> l
"This is a <test> file. It's <purpose> is to test <methods> for removing
HTML tags.\n"
>>> striptabs(l)
Tag found
Tag found
Tag found
"This is a  file. It's <purpose> is to test <methods> for removing HTML
tags.\n"
>>>


As you can see, it is finding all of the tags. Why isn't it replacing them?


Jon Cosby

jcosby@mindspring.com
www.jcosby.com