Dhruv Ghulati Dhruv Ghulati - 4 months ago 10
Python Question

Replace sequence of words in string with string python

I couldn't find anything that could solve this (

replace()
method doesn't work).

I have a sentence like:

sentence_noSlots = "Albania compared to other CountriesThe Internet users of Albania is similar to that of Poland , Portugal , Russia , Macedonia , Saudi Arabia , Argentina , Greece , Dominica , Azerbaijan , Italy with a respective Internet users of 62.8 , 62.1 , 61.4 , 61.2 , 60.5 , 59.9 , 59.9 , 59.0 , 58.7 , 58.5 -LRB- per 100 people -RRB- and a global rank of 62 , 63 , 64 , 65 , 66 , 68 , 69 , 70 , 71 , 72.10 years growthAlbania 's Internet users had a positive growth of 5,910 -LRB- % -RRB- in the last 10 years from -LRB- 2003 to 2013 -RRB- ."


I then have a string like:

extracted_country = Saudi Arabia
extracted_value = 58.5


I need to replace
Saudi Arabia
in the string with
<location>empty</location>
and
58.5
with
<number>empty</number>
. My current method is:

sentence_noSlots.replace(str(extracted_country),"<location>empty</location>")
sentence_noSlots.replace(str(extracted_value),"<number>empty</number>")


However because Saudi Arabia is two words, a simple word replace doesn't work. Nor does tokenizing first and replacing work due to the same type of issue:

sentenceTokens = sentence_noSlots.split()
for i,token in enumerate(sentenceTokens):
if token==extracted_country:
sentenceTokens[i]="<location>empty</location>"
if token==extracted_value:
sentenceTokens[i]="<number>empty</number>"
sentence_noSlots = (" ").join(sentenceTokens)


How can I achieve what I want to achieve?

Answer

string.replace() is not in-place. Strings are immutable in python.

From python docs:

string.replace(s, old, new[, maxreplace]) Return a copy of string s with all occurrences of substring old replaced by new. If the optional argument maxreplace is given, the first maxreplace occurrences are replaced.

Do this:

>>> sentence_noSlots = "Albania compared to other CountriesThe Internet users of Albania is similar to that of Poland , Portugal , Russia , Macedonia , Saudi Arabia , Argentina , Greece , Dominica , Azerbaijan , Italy with a respective Internet users of 62.8 , 62.1 , 61.4 , 61.2 , 60.5 , 59.9 , 59.9 , 59.0 , 58.7 , 58.5 -LRB- per 100 people -RRB- and a global rank of 62 , 63 , 64 , 65 , 66 , 68 , 69 , 70 , 71 , 72.10 years growthAlbania 's Internet users had a positive growth of 5,910 -LRB- % -RRB- in the last 10 years from -LRB- 2003 to 2013 -RRB- ."
>>> 
>>> extracted_country = "Saudi Arabia"
>>> extracted_value = 58.5
>>> s = sentence_noSlots.replace(str(extracted_country),"<location>empty</location>").replace(str(extracted_value),"<number>empty</number>")
>>> s
"Albania compared to other CountriesThe Internet users of Albania is similar to that of Poland , Portugal , Russia , Macedonia , <location>empty</location> , Argentina , Greece , Dominica , Azerbaijan , Italy with a respective Internet users of 62.8 , 62.1 , 61.4 , 61.2 , 60.5 , 59.9 , 59.9 , 59.0 , 58.7 , <number>empty</number> -LRB- per 100 people -RRB- and a global rank of 62 , 63 , 64 , 65 , 66 , 68 , 69 , 70 , 71 , 72.10 years growthAlbania 's Internet users had a positive growth of 5,910 -LRB- % -RRB- in the last 10 years from -LRB- 2003 to 2013 -RRB- ."