2016-08-04 7 views
0

Ich konnte nichts finden, das dieses Problem lösen könnte (replace() Methode funktioniert nicht).Ersetzen Reihenfolge der Wörter in String mit String Python

Ich habe einen Satz wie:

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

Ich habe dann eine Zeichenfolge wie:

extracted_country = Saudi Arabia 
extracted_value = 58.5 

Ich muß mit <location>empty</location> und 58.5 mit <number>empty</number>Saudi Arabia in der Zeichenfolge ersetzen. Meine aktuelle Methode ist:

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

Aber weil Saudi-Arabien zwei Wörter ist, funktioniert ein einfaches Wort ersetzen nicht. Auch nicht Tokenerstellung ersten und ersetzende Arbeit aufgrund der gleichen Art der Ausgabe:

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) 

Wie kann ich erreichen, was ich erreichen will?

Antwort

1

string.replace() ist nicht an Ort und Stelle. Zeichenfolgen sind in Python unveränderlich.

Von python docs:

string.replace (s, alt, neu [, maxreplace]) Gibt eine Kopie der Zeichenfolge s mit allen Vorkommen von durch neue ersetzt alte String. Wenn das optionale Argument maxreplace angegeben wird, werden die ersten maxreplace-Vorkommen ersetzt.

tun:

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

Ich nehme an, Sie gemeint:

extracted_country = "Saudi Arabia" 
extracted_value = "58.5" 

Dann arbeitet das .replace Verfahren wie erwartet. Seien Sie jedoch vorsichtig, es ist KEIN Modifikator: Es gibt eine NEUE Zeichenfolge mit der Änderung zurück. "sentence_noSlots" bleibt gleich.

Also beide von Verkettungs .replace Sie es wie folgt erreichen:

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