我正在尝试通过此代码从字符串中删除“not”并将“poor”替换为“good”
def replace(sth):
if 'poor' in sth :
sth=sth.replace('poor','good')
return sth
if 'not' in sth:
sth=sth.replace('not','')
return sth
print(replace("This is not poor"))
但输出是:
This is not good
我不明白为什么“不”没有被删除,而“差”被成功地替换为“好”
您可以使用 from sub() 正则表达式 're'
like : import re ... re.sub('not', '', sth, flags=re.IGNORECASE) // 不区分大小写 re.sub('poor', 'good', sth, flags=re.IGNORECASE ) ...
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句