码迷,mamicode.com
首页 > 编程语言 > 详细

python第八题 查找敏感单词

时间:2018-02-02 00:51:17      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:nbsp   readline   size   gpo   pen   file   line   decode   查找   

有一个问题: txt中只能是英文  只要是出现中文 就有问题 报错 ,肯定是编码的问题,但是这个问题我没有找到原因  之后再研究

敏感词文本文件 filtered_words.txt,里面的内容为以下内容,当用户输入敏感词语时,则打印出 Freedom,否则打印出 Human Rights。

代码:

def filterwords():
words = []
f = open(‘G://python文件//filtered_words.txt‘, ‘rb‘)
for l in f.readlines():
words.append(l.decode(‘utf-8‘)) #decode解码

iw = input(‘enter your words: ‘)
for w in range(len(words)):
if iw.find(words[w].strip()) > -1:
print(‘Freedom‘)
break
else:
print(‘Human Rights‘)
break

if __name__ == ‘__main__‘:
filterwords()
题目:敏感词文本文件 filtered_words.txt,里面的内容 和 0011题一样,当 用户输入敏感词语,则用 星号 * 替换, 例如当用户输入「北京是个好城市」,则变成「**是个好城市」。
def filterwords(iw):
words =[]
file = open(‘G://python文件//filtered_words.txt‘, ‘rb‘)
for f in file.readlines():
words.append(f.decode(‘utf-8‘))

for i in range(len(words)):
word = words[i].strip()
if iw.find(word) > -1:
return word
return ‘‘

def main():
iw = input(‘enter your words: ‘)
word = filterwords(iw)
if word != ‘‘:
print(iw.replace(word, ‘***‘))
else:
print(iw)

if __name__ == ‘__main__‘:
main()

python第八题 查找敏感单词

标签:nbsp   readline   size   gpo   pen   file   line   decode   查找   

原文地址:https://www.cnblogs.com/BUSYGIRL/p/8401576.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!