我有一个用户输入的字符串,我想搜索它并用替换字符串替换任何出现的单词列表。用python中的另一个字符串替换单词列表中的所有单词
import re
prohibitedWords = ["MVGame","Kappa","DatSheffy","DansGame","BrainSlug","SwiftRage","Kreygasm","ArsonNoSexy","GingerPower","Poooound","TooSpicy"]
# word[1] contains the user entered message
themessage = str(word[1])
# would like to implement a foreach loop here but not sure how to do it in python
for themessage in prohibitedwords:
themessage = re.sub(prohibitedWords, "(I'm an idiot)", themessage)
print themessage
上面的代码不起作用,我敢肯定我不明白python for循环是如何工作的。
你应该尝试检查出的蟒蛇spambayes实现可能更具可扩展性。 – dusual 2013-03-27 12:18:01