newbie string.replace question
Steve Holden
sholden at holdenweb.com
Wed Aug 8 18:44:43 EDT 2001
<fett at tradersdata.com> wrote in message
news:mailman.997305073.15200.python-list at python.org...
> Could someone please tell me why the following segment of code wont
> work:
>
> unformat = quote[11]
> string.replace(unformat, '<B>', '')
> string.replace(unformat, '</DL>', '')
> string.replace(unformat, '</B>', '')
> string.replace(unformat, '<DL COMPACT>', '')
> string.replace(unformat, '<DT>', '')
> string.replace(unformat, '<DD>', '')
> string.replace(unformat, '<OL>', '')
> string.replace(unformat, '</OL>', '')
>
> when i print the html stuff i was replacing is still there.
>
It doesn't work because you are throwing away the results of the
replacements instead of re-binding unformat to them. What you actually want
is probably:
unformat = quote[11]
unformat = string.replace(unformat, '<B>', '')
unformat = string.replace(unformat, '</DL>', '')
unformat = string.replace(unformat, '</B>', '')
unformat = string.replace(unformat, '<DL COMPACT>', '')
unformat = string.replace(unformat, '<DT>', '')
unformat = string.replace(unformat, '<DD>', '')
unformat = string.replace(unformat, '<OL>', '')
unformat = string.replace(unformat, '</OL>', '')
Although you might also consider:
unformat = quote[11]
for r in ('<B>', '</DL>', ... ,'</OL>):
unformat = unformat.replace(r, "")
This uses string methods (which are equivalent to the string functions, but
provide the subject string as an implicit first argument).
data-driven-is-more-flexible-ly y'rs - steve
--
http://www.holdenweb.com/
More information about the Python-list
mailing list