我正试图创建一个'审查员'读取“检查表”的程序。用语言来审查censorfile'。我觉得我很接近,但是当我运行该程序时,它只会审查一行。任何帮助将不胜感激!
#!/usr/bin/python
import sys, os
censorlist = raw_input("File with words to censor? ")
if os.path.isfile(censorlist):
print "The file", censorlist, "exists!"
else:
print "The file", censorlist, "doesn't exist!"
sys.exit()
print ""
filterword = {}
for currentline in censorlist:
line = currentline.strip()
filterword.append(line)
censorfile = raw_input("File to censor using provided list? ")
script = []
for currentline in censorfile:
line = currentline.strip()
script.append(line)
for lines in script:
results = []
words = lines.split()
for word in words:
if word in filter:
results.append("****")
else:
results.append(word)
resultfile = open(censorfile, 'w')
for items in results:
resultfile.write(items)
答案 0 :(得分:0)
您只需使用字符串的replace
方法替换子字符串的每个实例...
'foo bar spam foo'.replace('foo', '****') # '**** bar spam ****'
您只需要遍历单词列表进行审核,并为每个单词调用replace
。
for word in censorlist:
string.replace(word, '****')