Python替换功能不能替换所有单词

时间:2019-08-09 13:31:37

标签: python regex string replace

我有一个要从字符串中过滤掉的单词/短语的列表。但是,我的代码并没有过滤掉所有单词。为什么会这样?

stop_words = ['nbsp']
string = 'applicant nbsp entrepreneur nbsp develop level nbsp export artist nbsp export entrepreneur nbsp record label nbsp nbsp music publisher nbsp nbsp music manager'

for word in stop_words:
    if word in string:
        string = re.sub(" {} ".format(word), " ", string)
print(string)

运行此代码后,即为输出。

'applicant entrepreneur develop level export artist export entrepreneur record label nbsp music publisher nbsp music manager'

如您所见,'nbsp'仍然在字符串中。 另外,在我的停用词的实际列表中,列表中的元素超过了一个单词。例如,“睡觉”是一个元素。我也没有省略单词两边的空格,以免单字母的情况(例如“ a”)从包含“ a”的单词中过滤掉。

0 个答案:

没有答案
相关问题