String replace with return value? | | |
How can I do a "".replace operation which tells me if anything was
actually replaced?
Right now I am doing something like:
if searchTerm in text:
text = text.replace( searchTerm, 'other' )
But to me this seems inefficient since the 'in' operation will search
through the whole string, and then the 'replace' will as well.
The strings are quite large, they're whole web pages. Thanks for any help.
Roose | | | | re: String replace with return value?
On Mon, Oct 11, 2004 at 09:47:48AM +0000, Roose wrote:[color=blue]
> How can I do a "".replace operation which tells me if anything was
> actually replaced?[/color]
You could compare the strings from before and after the replacement.
[color=blue]
> Right now I am doing something like:
>
> if searchTerm in text:
> text = text.replace( searchTerm, 'other' )
>
> But to me this seems inefficient since the 'in' operation will search
> through the whole string, and then the 'replace' will as well.[/color]
Yep. Why not just omit the check and just do the replacement?
[color=blue]
> The strings are quite large, they're whole web pages. Thanks for
> any help.[/color]
Then I'd suggest to eventually switch to an existing templating
solution for HTML.
My guess is, however, that you're relatively new to Python. And in
this case, reinventing the wheel a few times is a good way to learn
the language and the libraries. We all did that ;-)
For your particular question, you can also use the re module to
perform string substitutions. And then, there's re.subn or the subn
method of regular expression objects, which will return both the
modified string and the number of substitutions.
Here's a short example for the subn method:
#############################################
import re
import time
modifiedRe = re.compile(r"\$modifieddate")
text = "this webpage was last modified on $modifieddate"
print text
text, replacements = modifiedRe.subn(time.asctime(), text)
print text
print replacements, "replacements made"
#############################################
HTH,
-- Gerhard | | | | re: String replace with return value?
Roose wrote:
[color=blue]
> How can I do a "".replace operation which tells me if anything was
> actually replaced?
>
> Right now I am doing something like:
>
> if searchTerm in text:
> text = text.replace( searchTerm, 'other' )[/color]
Try:
newtext = oldtext.replace(searchTerm, 'other')
if newtext is oldtext:
print "nothing changed"
else:
print "modified text"
You can almost certainly get away with using 'is' here since if replace
doesn't replace anything it simply returns the original string (this is an
implementation detail though, I don't know that there is a cast iron
guarantee that a future implementation won't copy the string if it hasn't
changed, although it would be pretty perverse if it did.) | | | | re: String replace with return value?
Duncan Booth wrote:
[color=blue][color=green]
>> How can I do a "".replace operation which tells me if anything was
>> actually replaced?
>> Right now I am doing something like:
>>
>> if searchTerm in text:
>> text = text.replace( searchTerm, 'other' )[/color]
>
> Try:
>
> newtext = oldtext.replace(searchTerm, 'other')
> if newtext is oldtext:
> print "nothing changed"
> else:
> print "modified text"
>
> You can almost certainly get away with using 'is' here since if replace
> doesn't replace anything it simply returns the original string[/color]
depends on how you define "if anything was actually replaced", of course:
[color=blue][color=green][color=darkred]
>>> mytext = "hello"[/color][/color][/color]
[color=blue][color=green][color=darkred]
>>> mytext is mytext.replace("l", "l")[/color][/color][/color]
False[color=blue][color=green][color=darkred]
>>> mytext == mytext.replace("l", "l")[/color][/color][/color]
True
[color=blue][color=green][color=darkred]
>>> mytext is mytext.replace("x", "x")[/color][/color][/color]
True[color=blue][color=green][color=darkred]
>>> mytext == mytext.replace("x", "x")[/color][/color][/color]
True
</F> | | | | re: String replace with return value?
Gerhard Haering wrote:[color=blue]
> On Mon, Oct 11, 2004 at 09:47:48AM +0000, Roose wrote:
>[color=green]
>>How can I do a "".replace operation which tells me if anything was
>>actually replaced?[/color]
>
>
> You could compare the strings from before and after the replacement.
>
>[color=green]
>>Right now I am doing something like:
>>
>>if searchTerm in text:
>> text = text.replace( searchTerm, 'other' )
>>
>>But to me this seems inefficient since the 'in' operation will search
>>through the whole string, and then the 'replace' will as well.[/color]
>
>
> Yep. Why not just omit the check and just do the replacement?[/color]
I thought of that, but don't you see, that's pretty much the same thing.
It's a question of doing an 'in' and a replace vs. a compare and a
replace. Both in and equality comparison are linear operations, so
choosing over the other doesn't really matter. A speedup can still be
obtained by traversing the string once instead of twice.
Now, the person who suggested using 'is' (which is constant time) was
onto something, but alas replace doesn't work like that. As someone
demonstrated, it returns a new string in any case.
[color=blue]
>
>[color=green]
>>The strings are quite large, they're whole web pages. Thanks for
>>any help.[/color]
>
>
> Then I'd suggest to eventually switch to an existing templating
> solution for HTML.[/color]
That doesn't apply, since I'm not generating web pages. I'm
transforming existing ones in a spider kind of program.
[color=blue]
> For your particular question, you can also use the re module to
> perform string substitutions. And then, there's re.subn or the subn
> method of regular expression objects, which will return both the
> modified string and the number of substitutions.[/color]
Thank you, that's just what I was looking for.
Roose | | | | re: String replace with return value?
Roose wrote:
[color=blue]
> I thought of that, but don't you see, that's pretty much the same thing.
> It's a question of doing an 'in' and a replace vs. a compare and a
> replace. Both in and equality comparison are linear operations, so
> choosing over the other doesn't really matter. A speedup can still be
> obtained by traversing the string once instead of twice.
>
> Now, the person who suggested using 'is' (which is constant time) was
> onto something, but alas replace doesn't work like that. As someone
> demonstrated, it returns a new string in any case.[/color]
No, it only returns a new string if it actually does a replacement.
Frederick Lundh confused the issue by pointing out that a replace can do a
replacement on a string but leave it unchanged. Provided you can guarantee
that the original string and replacement string are different this won't
matter.
To keep Frederick happy, you could change my example to:
if searchTerm == replacement:
newtext = oldtext
else:
newtext = oldtext.replace(searchTerm, replacement)
if newtext is oldtext:
print "nothing changed"
else:
print "modified text"
I will be surprised though if the overhead of comparing strings using '=='
even if they are large web pages if measurable in terms of either server
loading or time to retrieve a page. |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,471 network members.
|