我有以下带有方法的类:
class Trigger():
def getRidOfTrashPerSentence(self, line, stopwords):
countWord = 0
words = line.split()
for word in words:
if countWord == 0:
if word in stopwords:
sep = word
lineNew = line.split(sep, 1)[0]
countWord = countWord + 1
return(lineNew)
stopwords = ['regards', 'Regards']
def getRidOfTrash(self, aTranscript):
result = [self.getRidOfTrashPerSentence(line, self.stopwords) for line in aTranscript]
return(result)
我想用它实现的是在某些触发词(如['regards', 'Regards'])之后删除句子中的“垃圾”
所以当我插入这样的 block 时:
aTranScript = [ "That's fine, regards Henk", "Allright great"]
我正在寻找这样的输出:
aTranScript = [ "That's fine, regards", "Allright great"]
但是当我这样做时:
newFile = Trigger()
newContent = newFile.getRidOfTrash(aTranScript)
我只得到“没关系”
。
关于如何获得两个字符串的任何想法
最佳答案
这是一个简单的解决方案:
yourString = 'Hello thats fine, regards Henk'
yourString.split(', regards')[0]
此代码将返回:“你好,很好”
如果需要,您可以在末尾添加“问候”:
yourString.split(', 问候')[0]+', 问候'
关于python - 在触发词之后剪切句子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42221457/