我想在Python字符串的末尾替换字符。我有这个字符串:
s = "123123"
我想把最后一个2
替换成x
。假设有一个名为replace_last
的方法:
>>> replace_last(s, '2', 'x')
'1231x3'
有没有内置的或简单的方法来做到这一点?
这类似于Python的 str.replace()
:
>>> s.replace('2', 'x', 1)
'1x3123'
但是它是从末尾到开头的。
re.sub
替换字符串结尾的单词import re
s = "123123"
s = re.sub('23$', 'penguins', s)
print s
输出:
1231penguins
或者import re
s = "123123"
s = re.sub('^12', 'penguins', s)
print s
输出:
penguins3123
这正是 rpartition
函数的用途:
S.rpartition(sep)
->(head, sep, tail)
从字符串 S 的末尾开始查找分隔符 sep,返回它之前的部分、分隔符本身和它之后的部分。如果找不到分隔符,则返回两个空字符串和原字符串 S。
我编写了该函数以展示如何在您的用例中使用 rpartition
:
def replace_last(source_string, replace_what, replace_with):
head, _sep, tail = source_string.rpartition(replace_what)
return head + replace_with + tail
s = "123123"
r = replace_last(s, '2', 'x')
print r
输出:
1231x3
>>> s = '123123'
>>> t = s.rsplit('2', 1)
>>> u = 'x'.join(t)
>>> u
'1231x3'
或者
>>> 'x'.join('123123'.rsplit('2', 1))
'1231x3'
rsplit()
函数中第二个参数的值就可以从右侧进行多次替换。对我来说,这是一个优点,因为我在寻找解决此问题的方法时不必使用循环进行多次替换。 - Simonsep=None
并且 s
包含多个空格字符,则它不会像替换一样运行。例如:s = "1\n\n1"
,我们有 s.rsplit(None, 1) == ['1', '1']
而不是 s.rsplit('\n', 1) == ['1\n', '1']
。 - wjandrea>>> s = "aaa bbb aaa bbb"
>>> s[::-1].replace('bbb','xxx',1)[::-1]
'aaa bbb aaa xxx'
>>> s = "123123"
>>> s[::-1].replace('2','x',1)[::-1]
'1231x3'
replace()
正常语法进行正向和反向替换)。 - Daniel Goldfarbre.sub
可提供帮助。>>> import re
>>> s = "aaa bbb aaa bbb"
>>> s
'aaa bbb aaa bbb'
>>> re.sub('bbb$', 'xxx', s)
'aaa bbb aaa xxx'
>>>
>>> s = "aaa bbb aaa bbb"
>>> separator = " "
>>> parts = s.split(separator)
>>> separator.join(parts[:-1] + ["xxx"])
'aaa bbb aaa xxx'
更新
(在看到修改后的问题之后) 这是另一个非常具体的答案。
>>> s = "123123"
>>> separator = "2"
>>> parts = s.split(separator)
>>> separator.join(parts[:-1]) + "x" + parts[-1]
'1231x3'
更新2
有一种方法可以处理replace
,使其向后工作。
思路是使用[::-1]
反转字符串、要替换的文本和新文本。逻辑保持不变。
例如:
>>> a = "123 456 123 456 123 456"
我们希望用"abc"替换最后一个(或两个,或n个)出现的"123"
>>> a[::-1].replace("123"[::-1], "abc"[::-1], 1)[::-1]
'123 456 123 456 abc 456'
def replace_right(text, old, new, count=-1):
"""
Return a copy of text with all occurrences of substring old
replaced by new, starting from the right.
If count is given and is not -1, only the first count
occurrences are replaced.
"""
return text[::-1].replace(old[::-1], new[::-1], count)[::-1]
>>> replace_right(a, "123", "abc", 1)
'123 456 123 456 abc 456'
>>> replace_right(a, "123", "abc", 2)
'123 456 abc 456 abc 456'
有一种非常简单的方法来做到这一点,跟随我的步骤
s = "stay am stay am delete am"
# want to remove the last am
s = s[:s.rfind('am')]
print(s)
首先,我们找到am的最后一次出现的索引号,然后取0号索引到该特定索引。
str =“stay am stay am delete vvamvv gggg” #想要删除最后一个am str = str [: str.rfind('am')] +“REPLACED”+ str [(str.rfind('am')+ len(“am”):] print(str)
- Oishik Sinhastr
是一个糟糕的变量名,因为它遮蔽了Python内置的str
类型。最好使用更具描述性的名称,或者至少像s
这样的名称。我已经为您修复了它 :) 参见TypeError: 'list' object is not callable。 - wjandrea我得到了一个巧妙的答案,但效率不够高
fname = '12345.png.pngasdfg.png'
suffix = '.png'
fname_rev = fname[::-1]
suffix_rev = suffix[::-1]
fullname_rev = fname_rev.replace(suffix_rev, '', 1)
fullname = fullname_rev[::-1]
fullname '12345.png.pngasdfg'
str.replace(old, new, max_time)
因此您可以从原始字符串中删除最后匹配的字符串。rsplit
,因为它非常简单易用且直接实现了目标。以下是一个使用它的小函数:def replace_ending(sentence, old, new):
if sentence.endswith(old):
i = sentence.rsplit(old,1)
new_sentence =new.join(i)
return new_sentence
return sentence
print(replace_ending("aaa bbb aaa bbb", "bbb", "xxx"))
输出:
aaa bbb aaa xxx
r = re.sub(r"2[^2]$", "x", s)
- Ericre.sub(r"2([^2])$", r"x\1", s)
。虽然无论如何,由于字符类别的原因,这对于多字符替换都不起作用。 - wjandrea