python查找例题,python第八题 查找敏感单词(示例代码)

有一个问题: 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()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值