我需要从字符串中删除一个特定的单词。
但我发现Python的strip方法似乎无法识别有序的单词。它只会删除传递给参数的任何字符。
例如:
>>> papa = "papa is a good man"
>>> app = "app is important"
>>> papa.lstrip('papa')
" is a good man"
>>> app.lstrip('papa')
" is important"
如何使用Python去除指定的单词?
使用str.replace
方法。
>>> papa.replace('papa', '')
' is a good man'
>>> app.replace('papa', '')
'app is important'
或者使用re
并使用正则表达式。这将允许删除前导/尾随空格。
>>> import re
>>> papa = 'papa is a good man'
>>> app = 'app is important'
>>> papa3 = 'papa is a papa, and papa'
>>>
>>> patt = re.compile('(\s*)papa(\s*)')
>>> patt.sub('\\1mama\\2', papa)
'mama is a good man'
>>> patt.sub('\\1mama\\2', papa3)
'mama is a mama, and mama'
>>> patt.sub('', papa3)
'is a, and'
print "papa is papa is papa".replace('papa', '')
。 - thefourtheyere
的示例,这将允许去除前导/尾随空格。 - metatoasterpapa3
的使用(您应该测试papa3.replace('papa','') .strip()
)。 - metatoaster最简单的方法就是将其替换为空字符串。
s = s.replace('papa', '')
print "papa is papa is papa".replace('papa', '')
。 - thefourtheye如果我们谈论前缀和后缀,并且您的 Python 版本至少为 3.9,则可以使用这些新方法:
>>> 'TestHook'.removeprefix('Test')
'Hook'
>>> 'BaseTestCase'.removeprefix('Test')
'BaseTestCase'
>>> 'MiscTests'.removesuffix('Tests')
'Misc'
>>> 'TmpDirMixin'.removesuffix('Tests')
'TmpDirMixin'
string[string.startswith(prefix) and len(prefix):]
其中,string代表你的字符串变量,prefix代表你想要从字符串变量中删除的前缀。
例如:
>>> papa = "papa is a good man. papa is the best."
>>> prefix = 'papa'
>>> papa[papa.startswith(prefix) and len(prefix):]
' is a good man. papa is the best.'
你也可以使用正则表达式和re.sub
:
article_title_str = re.sub(r'(\s?-?\|?\s?Times of India|\s?-?\|?\s?the Times of India|\s?-?\|?\s+?Gadgets No'',
article_title_str, flags=re.IGNORECASE)
如果你知道想要替换的字符数组中每个单词的起始和结束索引值,并且只想替换特定的数据块,那么可以按照以下方式进行操作。
>>> s = "papa is papa is papa"
>>> s = s[:8]+s[8:13].replace("papa", "mama")+s[13:]
>>> print(s)
papa is mama is papa
或者,如果您还希望保留原始数据结构,可以将其存储在字典中。
>>> bin = {}
>>> s = "papa is papa is papa"
>>> bin["0"] = s
>>> s = s[:8]+s[8:13].replace("papa", "mama")+s[13:]
>>> print(bin["0"])
papa is papa is papa
>>> print(s)
papa is mama is papa
startswith
,它比正则表达式更容易理解。但是,我没有测试过正则表达式是否更快。>>> papa = "papa is a good man"
>>> app = "app is important"
>>> strip_word = 'papa'
>>> papa[len(strip_word):] if papa.startswith(strip_word) else papa
' is a good man'
>>> app[len(strip_word):] if app.startswith(strip_word) else app
'app is important'
最好的方法是
分割单词
使用if语句连接我们感兴趣的单词(您可以传入多个要删除的单词)
sentence = "papa is a good man"
' '.join(word for word in sentence.split() if word not in ['papa'])
请检查:
use replace()
------------
var.replace("word for replace"," ")
-----------------------------------
one = " papa is a good man"
two = " app is important"
one.replace(" papa ", " ")
output=> " is a good man"
two.replace(" app ", " ")
output=> " is important
S.lstrip
的文档吗?它的功能与你想要做的完全不同。你需要用replace
将这个字符串替换为空。 - Benjamin Bannierya
。让我们等待 OP 确认。 - thefourtheyeprint "papa is papa is papa".replace('papa', '')
,如果输出结果对您来说没问题,那么接受的答案是正确的。 - thefourtheyepapa.replace('man', '')
。您最终将得到'papa is a good '
(请注意'good'
后面的空格)。 - Ely Fialkoff