用字符串python替换字符串中的单词序列

时间:2016-08-04 14:41:05

标签: python string replace find findall

我无法找到任何可以解决此问题的方法(replace()方法无法解决)。

我有一句话:

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

我需要将字符串中的Saudi Arabia替换为<location>empty</location>,将58.5替换为<number>empty</number>。我目前的方法是:

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

然而,因为沙特阿拉伯是两个字,一个简单的单词替换不起作用。由于同一类型的问题,也没有先标记并替换工作:

 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)

我如何实现我想要达到的目标?

2 个答案:

答案 0 :(得分:1)

string.replace()不在原位。字符串在python中是不可变的。

来自python docs

  

string.replace(s,old,new [,maxreplace])返回字符串s的副本   所有出现的substring old都替换为new。如果是可选的   给出了参数maxreplace,第一个maxreplace出现次数   替换。

这样做:

>>> 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- ."

答案 1 :(得分:1)

我认为你的意思是:

extracted_country = "Saudi Arabia"
extracted_value = "58.5"

然后,.replace方法按预期工作。但是要小心,它不是修饰符:它返回带有修改的NEW字符串。 “sentence_noSlots”将保持不变。

所以通过链接两个.replace你可以像这样实现它:

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