I have something like this:
我有这样的事情:
text = 'This text is very very long.'
replace_words = ['very','word']
for word in replace_words:
text = text.replace('very','not very')
I would like to only replace the first 'very' or choose which 'very' gets overwritten. I'm doing this on much larger amounts of text so I want to control how duplicate words are being replaced.
我想只替换第一个'非常'或选择哪个'非常'被覆盖。我在更大量的文本上这样做,所以我想控制重复单词的替换方式。
3 个解决方案
#1
71
text = text.replace("very", "not very", 1)
>>> help(str.replace)
Help on method_descriptor:
replace(...)
S.replace (old, new[, count]) -> string
Return a copy of string S with all occurrences of substring
old replaced by new. If the optional argument count is
given, only the first count occurrences are replaced.
#2
16
text = text.replace("very", "not very", 1)
The third parameter is the maximum number of occurrences that you want to replace.
From the documentation for Python:
第三个参数是要替换的最大出现次数。从Python的文档:
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.string.replace(s,old,new [,maxreplace])返回字符串s的副本,其中所有出现的substring old都替换为new。如果给出了可选参数maxreplace,则替换第一个maxreplace事件。
#3
3
From http://docs.python.org/release/2.5.2/lib/string-methods.html :
来自http://docs.python.org/release/2.5.2/lib/string-methods.html:
replace( old, new[, count])
Return a copy of the string with all occurrences of substring old replaced by new. If the optional argument count is given, only the first count occurrences are replaced.replace(old,new [,count])返回字符串的副本,其中所有出现的substring old都替换为new。如果给出可选参数计数,则仅替换第一次计数。
I didn't try but I believe it works
我没试过,但我相信它有效
#1
71
text = text.replace("very", "not very", 1)
>>> help(str.replace)
Help on method_descriptor:
replace(...)
S.replace (old, new[, count]) -> string
Return a copy of string S with all occurrences of substring
old replaced by new. If the optional argument count is
given, only the first count occurrences are replaced.
#2
16
text = text.replace("very", "not very", 1)
The third parameter is the maximum number of occurrences that you want to replace.
From the documentation for Python:
第三个参数是要替换的最大出现次数。从Python的文档:
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.string.replace(s,old,new [,maxreplace])返回字符串s的副本,其中所有出现的substring old都替换为new。如果给出了可选参数maxreplace,则替换第一个maxreplace事件。
#3
3
From http://docs.python.org/release/2.5.2/lib/string-methods.html :
来自http://docs.python.org/release/2.5.2/lib/string-methods.html:
replace( old, new[, count])
Return a copy of the string with all occurrences of substring old replaced by new. If the optional argument count is given, only the first count occurrences are replaced.replace(old,new [,count])返回字符串的副本,其中所有出现的substring old都替换为new。如果给出可选参数计数,则仅替换第一次计数。
I didn't try but I believe it works
我没试过,但我相信它有效