[Tutor] string.replace

Ignacio Vazquez-Abrams ignacio@openservices.net
Tue, 11 Sep 2001 23:52:46 -0400 (EDT)


On Tue, 11 Sep 2001, Jon Cosby wrote:

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

Ypur function has a rather large logic error; it's only find the first tag
because you only go through the process once. You need to change the if to a
while and get rid of the embedded call to stribtabs().

-- 
Ignacio Vazquez-Abrams  <ignacio@openservices.net>