-1

For say if I have a paragraph and I wanna find and replace certain words in it with one certain word.

And I'm trying to do this using a for loop, after defining my word list.

Here's my code

script = """ In this sense, netting can represent , which gives Howie return on Zachary."""

ROE = ["In", "this"] #the word list I'm defining (the list of words I want it replaced)
for ROE in script: 
 script.replace(ROE, "ROE")

#desired output = ROE ROE sense, netting can represent , which gives Howie return on Zachary.

It doesn't really work, can someone help me fix it?

jaxan
  • 45
  • 5
  • 1
    With `for ROE in ...` you overwrite the previous content of `ROE`. – Michael Butscher Sep 11 '20 at 00:18
  • @Barmar but with that command, I have to keep repeating that I'm changing all the words to ROE (the word I want), and I'm just trying to simplify the code. Thank uuu – jaxan Sep 11 '20 at 00:22
  • 1
    Does this answer your question? [How to replace multiple substrings of a string?](https://stackoverflow.com/questions/6116978/how-to-replace-multiple-substrings-of-a-string) – The Grand J Sep 11 '20 at 00:23
  • In that question you want this answer https://stackoverflow.com/a/6117042/12672179 – The Grand J Sep 11 '20 at 00:23
  • IMO with was wrongly marked as answered elsewhere. The threads referred to are about multiple 1:1 replacements. The OP is asking how to do a many-to-one word replacement. – Stonecraft Oct 13 '22 at 03:43

4 Answers4

1

You have several problems:

  1. You're not looping over the list of words to replace, you're looping over the characters in script.
  2. You're not assigning the result of replace anywhere. It's not an in-place operation, since strings are immutable.
  3. You're reassigning the ROE variable.
for word in ROE:
    script = script.replace(word, 'ROE')

Note that replace() doesn't know anything about word boundaries. Your code will convert Inside to ROEside. If you want better, you can use regular expressions and wrap the words in \b boundaries. A regular expression would also allow you to perform all the replacements at once.

import re

regex = re.compile(r'\b(?:' + '|'.join(re.escape(word) for word in ROE) + r')\b')
script = regex.sub('ROE', script)

This creates a regular expression \b(?:In|this)\b, which matches either word.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Appreciate this! but I'm using an online interpreter and the import function is not really supported. that's why I'm trying to look for a simple code – jaxan Sep 11 '20 at 00:35
  • That's crazy. Python is totally crippled without its standard libraries. – Barmar Sep 11 '20 at 00:36
  • I knowww but I'm not using it that much so don't want to go thru installing everything :)) thanks for the help man – jaxan Sep 11 '20 at 00:54
  • Are you sure they don't automatically import some of the common libraries like `re`? Can't you use a different interpreter, such as ideone.com or repl.it? – Barmar Sep 11 '20 at 00:55
  • Uhhh not exactly sure. I know repl.it supports importing libraries, but my main goal for this code is to copy the output after the code done everything I wanted, and I think repl.it doesn't let me copy the output properly or smth //// anyways managed to found an solution so that's good! I'm kinda new to this site, can I ask if I should just press solved on my own solution posted or it'll automatically take it as solved when the original person who posted the question gives an answer? – jaxan Sep 11 '20 at 01:01
  • I just tried it. I was able to `import re` and I was able to copy the output. – Barmar Sep 11 '20 at 01:03
  • Ohh ok! I might be doing smth wrong but it tells me regeg is not defined? – jaxan Sep 11 '20 at 01:27
  • It's just a typo, for `regex` – Barmar Sep 11 '20 at 01:31
0
script = """ In this sense, netting can represent , which gives Howie return on Zachary."""


ROE = ["In", "this"] #the word list I'm defining (the list of words I want it replaced)
for word in ROE: 
    script = script.replace(word, "ROE")

print(script)

Output:

ROE ROE sense, netting can represent , which gives Howie return on Zachary.

i.e. identical with your desired one.

desertnaut
  • 57,590
  • 26
  • 140
  • 166
Alex Solovyov
  • 86
  • 1
  • 3
0

The string str data type in Python is immutable. This means that if you want to change a string, you basically have to create a new string that has the changes and then you can assign the result to a variable.

Of course, you can assign the result to the same variable the original string was assigned to, which may have had the last reference to the old string, causing it to get cleaned up. But for a brief moment, there will always be a new copy of the string.

For example:

s = 'Hello'
s += ' world!'
print(s)

This seem to add ' world!' onto the existing s with 'Hello', but it really just creates a new string 'Hello world!' and assigns that to s, replacing the old one.

In your case, this explains why you can't just call .replace() on a string and expect it to change. Instead, that method returns the new string you want and you can assign it to a variable:

script = """ In this sense, netting can represent , which gives Howie return on Zachary."""

roe = ["In", "this"]
for word_to_replace in roe: 
    script = script.replace(word_to_replace, 'ROE')

(note that there were some other issues as well, but the above should work)

Grismar
  • 27,561
  • 4
  • 31
  • 54
  • I ran the code and it still doesn't work :(( but thank u! – jaxan Sep 11 '20 at 00:36
  • 1
    Not sure what code you ran - I just copied and pasted the code from the answer into a Python file, added `print(script)` and the output was `ROE ROE sense, netting can represent , which gives Howie return on Zachary.` What else did you expect? – Grismar Sep 11 '20 at 03:18
  • yeah thanks for the help mate. I'm just too dumb to realise I need to add the print ._. – jaxan Sep 11 '20 at 09:02
  • Happy to help - if this answers the question, please accept the answer with the check mark, so others can find it an people don't look at the question to answer it – Grismar Sep 11 '20 at 10:48
0

I found a solution that is relatively easy

stopwords=['In','this','to']
for i in stopwords:
 n=a.replace(i,'ROE')
 a=n

and I was helped by this link: Removing list of words from a string

desertnaut
  • 57,590
  • 26
  • 140
  • 166
jaxan
  • 45
  • 5