我有一個用戶輸入的字符串,我想搜索它並用替換字符串替換任何出現的單詞列表。用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